GroganJ
asked on
SQL Server - string parsing
Hi,
I am passing a string to SQL Server 2005 that I want broken down into constituent words, where the delimiter is a comma.
For example, the input string might be "the, cat, in, the, hat", which I want broken down into the components to be inserted into a table.
Any thoughts on how to do this?
I am passing a string to SQL Server 2005 that I want broken down into constituent words, where the delimiter is a comma.
For example, the input string might be "the, cat, in, the, hat", which I want broken down into the components to be inserted into a table.
Any thoughts on how to do this?
You have to write function for that.
This is the funciton I am using.
SELECT * FROM [fn_Split]('the, cat, in, the, hat',',')
will return splited string as a table
Raj
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
--select * from [fn_Split]('str,asf',',')
ALTER function [dbo].[fn_Split](
@String nvarchar (4000),
@Delimiter nvarchar (10)
)
returns @ValueTable table ([Value] nvarchar(4000))
begin
declare @NextString nvarchar(4000)
declare @Pos int
declare @NextPos int
declare @CommaCheck nvarchar(1)
--Initialize
set @NextString = ''
set @CommaCheck = right(@String,1)
--print @CommaCheck
--Check for trailing Comma, if not exists, INSERT
--if (@CommaCheck <> @Delimiter )
set @String = @String + @Delimiter
--Get position of first Comma
set @Pos = charindex(@Delimiter,@String)
set @NextPos = 1
--Loop while there is still a comma in the String of levels
while (@pos <> 0)
begin
set @NextString = substring(@String,1,@Pos - 1)
insert into @ValueTable ( [Value]) Values (@NextString)
set @String = substring(@String,@pos +1,len(@String))
set @NextPos = @Pos
set @pos = charindex(@Delimiter,@String)
end
return
end
SELECT * FROM [fn_Split]('the, cat, in, the, hat',',')
will return splited string as a table
Raj
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
The above functions will work, but performance-wise it is much quicker to do it with the help of a Numbers table in your database. For large datasets I've found there's a huge difference in performance. The principle is explained on the following webpage, and the example of what you want to do is under the heading 'parsing a string':
http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html
http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html
ASKER
Excellent link - that solved my problem perfectly.
so, my link didn't help :)
glad we could help !
CHeers
glad we could help !
CHeers
declare @string varchar(100)
set @string = 'the, cat, in, the, hat'
declare @position int
declare @piece varchar(100)
if right(rtrim(@string),1) <> ','
set @string = @string + ','
set @position = patindex('%,%' , @string)
while @position <> 0
begin
set @piece = left(@string, @position - 1)
print cast(@piece as varchar(100))
set @string = stuff(@string, 1, @position, '')
set @position = patindex('%,%' , @string)
end
set @string = 'the, cat, in, the, hat'
declare @position int
declare @piece varchar(100)
if right(rtrim(@string),1) <> ','
set @string = @string + ','
set @position = patindex('%,%' , @string)
while @position <> 0
begin
set @piece = left(@string, @position - 1)
print cast(@piece as varchar(100))
set @string = stuff(@string, 1, @position, '')
set @position = patindex('%,%' , @string)
end
https://www.experts-exchange.com/A_1536.html
can be easily put with a INSERT ... SELECT ... FROM dbo.ParmsToList( ... ) as needed