Hi,
Today one of my colleagues in company faced a problem, He wants to split a text field by a delimiter character.
This motivate me to write a Split function for my own use and put it here for help others.
Suppose that our Primary Data is like this image and delimited by single slash ( / )
I created a Scalar Value T-SQL Function to create the string to be used in select statement, so the function will return a string which should be used in a select statement and the function itself doesn’t split data. result of this function should be used in a dynamic t-sql to return result as expected.
This is script of the function:
— ================================================
— This Function will generate script to be used
— in a select statement and fetch every delimited
— as a column
— Use it in this way: (Example)
— declare @sql varchar(max)
— set @sql=’select ‘+dbo.SplitFieldByDelimiter(‘ZoneName’,’/’,8)
— +’ from [TVNZ_NDS].[Staging].[tmpImpressionExtract]’
—
— exec (@sql)
— ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
— =============================================
— Author: Reza Rad
— Version: 1.0
— Create date: 21/02/2012
— Description: To Split a Delimited field by a Delimiter
— =============================================
CREATE FUNCTION dbo.SplitFieldByDelimiter
(
@fieldname varchar(max)
,@delimiter varchar(max)
,@delimiter_count int
)
RETURNS varchar(max)
AS
BEGIN
— variable declaration
declare @first_index varchar(max)
,@next_index varchar(max)
,@sql_statement varchar(max)
,@term_statement varchar(max)
,@length varchar(max)
,@iterator int
— initialization
set @next_index=-1
set @iterator=1
set @sql_statement=’ ‘
— code
while(@iterator<=@delimiter_count)
begin
set @first_index=@next_index+’+1′
set @next_index=’CHARINDEX(”’+@delimiter+”’,’+@fieldname+’,’+@first_index+’)’
if (@iterator=@delimiter_count)
begin
set @next_index=’LEN(‘+@fieldname+’)+1′
end
set @length=@next_index+’-(‘+@first_index+’)’
set @term_statement=’SubString(‘+@fieldname+’,’+@first_index+’,’+@length+’)’
set @term_statement=’case when ‘+@next_index+’>0 then ‘+@term_statement+’ else ”nothing” end as Column’+CONVERT(varchar(max),@iterator)
if (@iterator=1)
begin
set @sql_statement=@term_statement
end
else
begin
set @sql_statement=@sql_statement+’, ‘+@term_statement
end
set @iterator=@iterator+1
end
— Return the result of the function
RETURN @sql_statement
END
GO
and This is they way we use it :
declare @sql varchar(max)
set @sql=’select top 1000 ‘+dbo.SplitFieldByDelimiter(‘DelimitedText’,’/’,8)
+’ from [TmpDataTable]’
exec (@sql)
So result will be like this :
In this version of Split function you need to enter number of occurrence of delimiter as function parameters, but I will solve it soon and publish version 2 without need for occurrence.
Hope it helps you all 🙂
P.S:
I created a CodePlex project for this Split function to work on it further.
CodePlex T-SQL Split url is :
1 thought on “T-SQL Split Function : Split Field By a Delimiter”