| Author |
Topic |
|
treeskin
Starting Member
22 Posts |
Posted - 2006-05-03 : 20:31:59
|
| I would need to get a left outer join for the comparison, but my intention is to use Table A as the master table that ending with A, and search in Table B for any Product ID that have the same prefix without the A. I tried on Substring for the Product ID but doesn't work well.What are the ways to get the below results?The result will be Table A = 1234A, and it should return value 1234 from Table B.Table A Product ID-----------1234A5678A9834A34567ATable BProduct ID-----------12343456787369834The end results will be showing:Table A Table BProduct ID Product ID---------- -----------1234A 12345678A 9834A 983434567A 34567Thanks. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-03 : 20:50:20
|
[code]declare @tableA table( ProductID varchar(20))declare @tableB table( ProductID varchar(20))insert into @tableAselect '1234A' union allselect '5678A' union allselect '9834A' union allselect '34567A'insert into @tableBselect '1234' union allselect '34567' union allselect '8736' union allselect '9834'select a.ProductID, isnull(b.ProductID, '')from @tableA a left join @tableB b on a.ProductID like b.ProductID + '%'[/code] KH |
 |
|
|
treeskin
Starting Member
22 Posts |
Posted - 2006-05-04 : 01:55:36
|
| Hi KH, why there is a need of UNION ALL while insert the value to the table? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-04 : 01:57:17
|
The section in green is for me to generate the data for testing of the query in blue KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-04 : 04:05:09
|
| Read about Union in sql server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
treeskin
Starting Member
22 Posts |
Posted - 2006-05-23 : 23:17:10
|
| KH and Madhivanan, what I am asking is why we need a UNION ALL? As we could just insert the value into the table. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-23 : 23:22:21
|
Yes. You can just insert directly into the table as below without the UNION ALL.insert into @tableA select '1234A'insert into @tableA select '5678A' Using UNION ALL saves me some typing or copy & paste of the 'insert into ...'Is this what you wanted to know ? KH |
 |
|
|
treeskin
Starting Member
22 Posts |
Posted - 2006-05-23 : 23:25:00
|
| And is there other way to perform the same intention? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-23 : 23:36:33
|
quote: Originally posted by treeskin And is there other way to perform the same intention?
This is the simplest direct way of creating / inserting sample data into a table. KH |
 |
|
|
treeskin
Starting Member
22 Posts |
Posted - 2006-05-24 : 01:47:33
|
| ok... thanks KH.. it was working for simple join. |
 |
|
|
marat
Yak Posting Veteran
85 Posts |
Posted - 2006-05-24 : 01:58:07
|
| declare @tableA table( ProductID varchar(20))declare @tableB table( ProductID varchar(20))insert into @tableAselect '1234A' union allselect '5678A' union allselect '9834A' union allselect '34567A' union allselect '3456A'insert into @tableBselect '1234' union allselect '34567' union allselect '8736' union allselect '9834' union allselect '345'--this is wrongselect a.ProductID, isnull(b.ProductID, '')from @tableA a left join @tableB b on a.ProductID like b.ProductID + '%'--this would be rightselect a.ProductID, isnull(b.ProductID, '')from @tableA a left join @tableB b on replace(a.ProductID,'A','') = b.ProductID |
 |
|
|
|