Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Nosediver
Starting Member
7 Posts |
Posted - 2007-10-10 : 02:32:34
|
Hi,I am running into another non standard problem:I have 2 tables, that have to be linked with a certain code. The problem is that in Table A it looks like this:XX4536 while XX is fixed and the numbers are variableXX6435XX1234...Table B looks like this:XX45XX5326XX453687XX3456...Now I need a function to link:XX4536 with XX453687 -> solved by restricting the field size of Table B to 6 charactersXX4536 with XX45 -> Here I have no idea how to do it. What I need is to match all Fields from Table A with all fields from Table B that are 4 characters long and begin with e.g. 45 in this case.XX3425 with XX3425 -> is no problemThis is my current code: (I avoided the join function while I was trying aroundSELECT A.[4DigitGCC], A.SupplierCapabilitySEA, A.[GLOBAL COMMODITY CODE DESCRIPTION (ENGLISH)], B.Description, B.Rating, B.Criteria, B.GSC, B.GCCFROM 4x4 AS A, ASDBComplete AS BWHERE (((A.[4DigitGCC]) = [B].[GCC]));I would need something like 'includes' instead of the '='I tried the 'LIKE' function but it didn't work.Hope you have an idea,Lars |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-10 : 03:11:24
|
[code]-- Prepare sample dataDECLARE @TableA TABLE (Code VARCHAR(10))INSERT @TableASELECT 'XX4536' UNION ALLSELECT 'XX6435' UNION ALLSELECT 'XX1234'DECLARE @TableB TABLE (Code VARCHAR(10))INSERT @TableBSELECT 'XX45' UNION ALLSELECT 'XX5326' UNION ALLSELECT 'XX453687' UNION ALLSELECT 'XX3456'-- Peso 1SELECT *FROM @TableA AS aLEFT JOIN @TableB AS b ON b.Code LIKE a.Code + '%'-- Peso 2SELECT *FROM @TableA AS aLEFT JOIN @TableB AS b ON LEFT(b.Code, LEN(a.Code)) = a.Code[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|