Created
May 12, 2022 16:04
-
-
Save jingyang-li/36f1a598fcea846d38a3aada74ecccba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Yitzhak Khabinsky's solution | |
--https://docs.microsoft.com/en-us/answers/questions/845428/need-sql-help-to-split-comma-separated-value-into.html | |
create table #test | |
(col VARCHAR(2000)) | |
insert into #test | |
values('2292,0192,20211018,0778988158494,0000600,150,000000000000001') | |
,('2292,14642,20210923,630996500903,0000500,061,1') | |
,('2292,4164,20211021,4006592590505,0002180,150,000000000000001,EUR,21,0000000,501,1,21-01917') | |
--DECLARE @separator CHAR(1) = ','; | |
SELECT c.value('(/root/r[1]/text())[1]', 'INT') AS col1 | |
, c.value('(/root/r[2]/text())[1]', 'VARCHAR(10)') AS col2 | |
, c.value('(/root/r[3]/text())[1]', 'DATE') AS col3 | |
, c.value('(/root/r[3]/text())[1]', 'DATE') AS col4 | |
, c.value('(/root/r[3]/text())[1]', 'DATE') AS col5 | |
, c.value('(/root/r[3]/text())[1]', 'DATE') AS col6 | |
, c.value('(/root/r[3]/text())[1]', 'DATE') AS col7 | |
, c.value('(/root/r[3]/text())[1]', 'DATE') AS col8 | |
, c.value('(/root/r[3]/text())[1]', 'DATE') AS col9 | |
, c.value('(/root/r[3]/text())[1]', 'DATE') AS col10 | |
, c.value('(/root/r[3]/text())[1]', 'DATE') AS col11 | |
, c.value('(/root/r[3]/text())[1]', 'DATE') AS col2 | |
, c.value('(/root/r[13]/text())[1]', 'VARCHAR(20)') AS col13 | |
FROM #test AS t | |
CROSS APPLY (SELECT TRY_CAST('<root><r><![CDATA[' + | |
REPLACE(col, ',', ']]></r><r><![CDATA[') + | |
']]></r></root>' AS XML)) AS t1(c); | |
Drop table #test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment