October 21, 2014 at 3:45 am
Hi
I have a comma separated field containing numerous 2 digit numbers that I would like splitting out by a corresponding unique code held in another field on the same row.
E.g
Unique Code Comma Separated Field
14587934 1,5,17,18,19,40,51,62,70
6998468 10,45,62,18,19
79585264 1,5,18
These needs to be in column format or held in an array to be used as conditional criteria.
Unique Code Comma Separated Value
79585264 1
79585264 5
79585264 18
Thanks,
October 21, 2014 at 4:02 am
Take a look at this excellent splitter function by Jeff Moden[/url].
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
November 24, 2014 at 3:25 am
hi!
kindly go through below link
http://msdn.microsoft.com/en-IN/library/ms178129.aspx
there is provision of bulk insert and bcp which is ultra fast in execution
Regards
Rajani Karthik
November 24, 2014 at 10:09 pm
greatrajani (11/24/2014)
hi!kindly go through below link
http://msdn.microsoft.com/en-IN/library/ms178129.aspx
there is provision of bulk insert and bcp which is ultra fast in execution
Regards
Rajani Karthik
While I applaud the innovative thought, that would mean that you would have to first export the table to a file, import the file into a table, and then unpivot the table to get the desired output of 1 row per element. That would hardly be an "ultra fast" execution.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 24, 2014 at 10:09 pm
@ Trawler,
Are you all set on this one?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 24, 2014 at 10:10 pm
Thank you for the kudo, Koen. :blush:
--Jeff Moden
Change is inevitable... Change for the better is not.
April 20, 2017 at 2:13 am
The answer is this if you have SQL Server 2016 STRING_SPLIT ( string , separator )
Prior to SQL Server 2016 the answer for me was FnSplitFieldOnComma as below...
USE [XXX]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
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 return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end;
April 20, 2017 at 2:19 am
Jeff 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 return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end;]
April 20, 2017 at 11:23 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;]
All the way back in SQL 2008 (and ever since), Jeff's string splitter function was blowing the performance doors off of code like that (for prior to the SQL 2016 STRING_SPLIT function). If you still have that older code in place, it would be far superior to replace it with Jeff's DelimitedSplit8K function.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
April 20, 2017 at 3:11 pm
Trawler - Thursday, April 20, 2017 2:13 AMThe answer is this if you have SQL Server 2016 STRING_SPLIT ( string , separator )
Prior to SQL Server 2016 the answer for me was FnSplitFieldOnComma as below...USE [XXX]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GOALTER 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;
That method is what is known as a "Split Nibbler". Here's a performance chart on various types of splitters. The "Split Nibbler" method is the Blue line. The thin Black line with the label of "????" is the splitter that Koen and Steve are talking about and that's before an additional optimization.
--Jeff Moden
Change is inevitable... Change for the better is not.
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.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply