SQLUSA
SQL 2005
Business Intelligence Workshop
NYC SEP 22-25
Register Today!

Microsoft SQL Server 2008 Best Practices

How to split CSV list inline fast?

 

The following T-SQL database script in Query Editor uses recursive CTE to split a CSV list inline fast.

 

declare @CSV varchar(128)

set @CSV = 'Seattle Steel Structures,720 4th Avenue,,,Kirkland,WA,98233,425-729-5600,425-729-5601';

 

with cteCSVSplit as

(

select

Element=left(@CSV, charindex(',',@CSV) - 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

 

 

 

SQLUSA - The Best SQL Server 2008 Training in the World
 
 
SQLUSA.com Home Page