Long island web design, hosting company
Home About Us Support Products Services Portfolio Contact Us Home

TSQL > Spliting Delimited Data in T-SQL

Created On: 1/18/2004 8:51:00 AM

Use the following code to split data in TSQL

DECLARE @cData as varchar(100)
DECLARE @Sep as char(1)
DECLARE @arryval as varchar(100)
DECLARE @Pos as int
Set @cData = '1,2,3,4'
Set @cData = @cData + ','
Set @Sep = ','

while patindex ('%' + @Sep + '%', @cData) <> 0
begin
select @pos = patindex ('%' + @Sep + '%', @cData)
select @arryval = left(@cData, @pos - 1)
print @arryval
select @cData = Stuff (@cData,1,@pos,'')
end