Labels

Monday, January 2, 2012

Split CSV Input

-- T-SQL delimited list splitter - split string list
DECLARE @CSV varchar(128) 
      = 'Seattle Steel Structures,720 4th Avenue,,,Kirkland,WA,98233,
      425-729-5600,425-729-5601'; 
 
 
;WITH ctecsvsplit 
     AS (SELECT Element = Left(@CSV,NULLIF(Charindex(',',@CSV),0) - 1), 
                runningcsv = Stuff(@CSV + ',',1,Charindex(',',@CSV),'') 
         UNION ALL 
         SELECT Convert(VARCHAR(128),Left(runningcsv,Charindex(',',runningcsv) - 1)), 
                Stuff(runningcsv,1,Charindex(',',runningcsv),'') 
         FROM   ctecsvsplit 
         WHERE  runningcsv <> '') 
SELECT Element 
FROM   ctecsvsplit 
GO 
/*
Element
Seattle Steel Structures
720 4th Avenue
 
 
Kirkland
WA
98233
        425-729-5600
425-729-5601
*/

No comments:

Post a Comment