

T-SQL Join on the foreign key that has zero
source link: https://www.codesd.com/item/t-sql-join-on-the-foreign-key-that-has-zero.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

T-SQL Join on the foreign key that has zero
I need to link various tables that each have a common key (a serial number in this case). In some tables the key has a leading zero e.g. '037443' and on others it doesn't e.g. '37443'. In both cases the serial refers to the same product. To confound things serial 'numbers' are not always just numeric e.g. may be "BDO1234", in these cases there is never a leading zero.
I'd prefer to use the WHERE statement (WHERE a.key = b.key) but could use joins if required. Is there any way to do this?
I'm still learning so please keep it simple if possible. Many thanks.
Based on the accepted answer in this link, I've written a small tsql sample to show you what I meant by 'the right direction':
Create the test table:
CREATE TABLE tblTempTest
(
keyCol varchar(20)
)
GO
Populate it:
INSERT INTO tblTempTest VALUES
('1234'), ('01234'), ('10234'), ('0k234'), ('k2304'), ('00034')
Select values:
SELECT keyCol,
SUBSTRING(keyCol, PATINDEX('%[^0]%', keyCol + '.'), LEN(keyCol)) As trimmed
FROM tblTempTest
Results:
keyCol trimmed
-------------------- --------------------
1234 1234
01234 1234
10234 10234
0k234 k234
k2304 k2304
00034 34
Cleanup:
DROP TABLE tblTempTest
Note that the values are alpha-numeric, and only leading zeroes are trimmed.
One possible drawback is that if there is a 0
after a white space it will not be trimmed, but that's an easy fix - just add ltrim
:
SUBSTRING(LTRIM(keyCol), PATINDEX('%[^0]%', LTRIM(keyCol + '.')), LEN(keyCol)) As trimmed
Recommend
-
8
Showing full MySQL foreign key errors By continuing your visit to this site, you accept the use of cookies. Read more. Scout APM helps...
-
11
I do not see a foreign key index I created advertisements I've created a table with a unique index as well as a foreign index. When I add the uniqu...
-
10
Create foreign key after normalization advertisements I've been creating a database, and I've just completed the normalization step. One of th...
-
13
How do I know if a property of an EntityObject is a primary key or a foreign key? advertisements Suppose that I have a class generated by Enti...
-
1
Showing full MySQL foreign key errors Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start y...
-
6
Code is available on GitHub What are foreign keys A foreign key is a property in a table that references items in another table. That’s why they ar...
-
5
SQL language proposal: JOIN FOREIGN The idea is to improve the SQL language, specifically the join syntax, for the special but common case when joining on foreign key columns. The problem Example below taken from Post...
-
13
Join 2 primary key columns to 1 foreign key column in the same table advertisements I have a jobs...
-
12
Do you wonder if MySQL tells you the truth about writes to...
-
9
Foreign Key Constraint Problem for One-to-Many Unidirectional Relationship advertisements I have Employee (parent)...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK