Home Forums SQL Server 2014 Development - SQL Server 2014 Split out a field of comma separated values based on a unique code in the same row. RE: Split out a field of comma separated values based on a unique code in the same row.
April 24, 2017 at 2:26 am
Trawler - Thursday, April 20, 2017 2:19 AMJeff Moden - Monday, November 24, 2014 10:09 PM@ Trawler,Are you all set on this one?The answer is this if you have SQL Server 2016 STRING_SPLIT ( string , separator )
Prior to SQL Server 2016 the answer for me was a function named FnSplitFieldOnComma .[/ALTER FUNCTION [dbo].[fnSplitFieldOnComma](@String varchar(MAX), @Delimiter char(1))
returns @temptable TABLE (items varchar(MAX))
as
begin
declare @idx int
declare @slice varchar(8000)select @idx = 1
if len(@String)<1 or @String is null returnwhile @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @Stringif(len(@slice)>0)
insert into @temptable(Items) values(@slice)set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end;]
Thanks for the replies.