July 25, 2019 at 5:14 pm
Hi All:
Need help with query. I have data in this format:
create table test
(name varchar(1000),
numbers varchar (100))
insert into test values ('ABC PQR; XYZ ABC;','312; 435;')
insert into test values ('SQL; Windows; Unix;','456; 345;')
insert into test values ('Tom John;','254; 376;')
I want the output to be like this:
Thanks!
July 25, 2019 at 5:45 pm
Look into STRING_SPLIT() function:
https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2017
--Vadim R.
July 25, 2019 at 6:31 pm
I had tried string_function, but that only output single column.
Thanks
July 25, 2019 at 7:01 pm
Does this get you started?
SELECT *
FROM test T
CROSS APPLY DelimitedSplit8K(T.name, ';') NM
CROSS APPLY DelimitedSplit8K(T.numbers, ';') NU
WHERE NM.ItemNumber = NU.ItemNumber
AND NM.ItemValue <> ''
Do a search on this site for "DelimitedSplit8k"
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
July 25, 2019 at 7:50 pm
thanks for the head start... I was curious, so I finished it.
SELECT nm.item,
Name = NU.Item
FROM test T
CROSS APPLY SCRIDB.dbo.DelimitedSplit8K(T.name, ';') NM
CROSS APPLY SCRIDB.dbo.DelimitedSplit8K(T.numbers, ';') NU
WHERE NM.ItemNumber = NU.ItemNumber
AND NOT (nm.Item ='' AND nu.item ='');
July 25, 2019 at 8:31 pm
One thing. This is not a great design.
I would recommend trying to change the design on these tables. Or, at minimum, create a regular extract of these tables into another set of tables if the design cannot be modified
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
July 25, 2019 at 9:13 pm
Amen to that! It's a truly awful design. To the OP - performance of queries against a table like this are going to be painfully slow. You should be able to use DelimitedSplit8K to split up the values in the columns and then insert the results into a permanent table - and index it.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy