June 12, 2018 at 10:16 am
One table has a Supplier Code, which is always 5 Alpha-Numeric.
I need to join on a table where that Supplier code is embedded in a string.
the string is always formatted as such:
XXXXXXXX-Supplier-ZZZZ (the prefix is variable length)
the number of characters in the prefix is not consistent, so i can't do a substring.
In Excel, i could do a FIND, then add 1, and pull the next 5 characters.
The suffix needn't come into play.
I can't get his to work in SQL (TBH, i don't know how) lol
thanks
June 12, 2018 at 10:27 am
WHERE/ON SUBSTRING(string, CHARINDEX('-', string) + 1, 5) = ...
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
June 12, 2018 at 10:30 am
Thanks Scott!
I will review the function CHARINDEX!
Solved
June 12, 2018 at 12:14 pm
ALTER TABLE [YourSchema].[YourTable]
ADD Supplier AS CAST(SUBSTRING([YourStringColumn], CHARINDEX('-', [YourStringColumn]) + 1, 5) AS CHAR(5)) PERSISTED;
Now you can index the [Supplier] field, and join on it directly.
June 12, 2018 at 12:30 pm
If this is needed on a regular basis, I recommend a persisted computed column that can be indexed.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 12, 2018 at 1:06 pm
Or, better yet, just go back to properly using 1NF in the table and store all values as atomic, i.e., individual columns. The computed column should be the concatenated value, not the other way around.
--XXXXXXXX-Supplier-ZZZZ
...
Xcolumn char(8) NOT NULL,
Supplier char(5) NOT NULL,
Zcolumn char(4) NOT NULL,
Concat_Column AS CAST(Xcolumn + '-' + Supplier + '-' + Zcolumn AS char(19),
...
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
June 12, 2018 at 1:11 pm
ScottPletcher - Tuesday, June 12, 2018 1:06 PMOr, better yet, just go back to properly using 1NF in the table and store all values as atomic, i.e., individual columns. The computed column should be the concatenated value, not the other way around.
Amen to that!
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply