Solved

SQL Server - string parsing

Posted on 2010-11-15
9
567 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:GroganJ
9 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34135433
please read this article on how to do the split part.
http://www.experts-exchange.com/A_1536.html

can be easily put with a INSERT ... SELECT ... FROM dbo.ParmsToList( ... ) as needed
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34135445
You have to write function for that.
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34135492
This is the funciton I am using.
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

Open in new window


SELECT * FROM [fn_Split]('the, cat, in, the, hat',',')
will return splited string as a table

Raj
0
 
LVL 6

Accepted Solution

by:
subhashpunia earned 500 total points
ID: 34135584
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 6

Expert Comment

by:dan_mason
ID: 34135838
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 
0
 

Author Closing Comment

by:GroganJ
ID: 34135906
Excellent link - that solved my problem perfectly.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34135923
so, my link didn't help :)

glad we could help !
CHeers
0
 
LVL 3

Expert Comment

by:expert_dharam
ID: 34136182
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
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34136209
I also feel to comment like AngelIII did :)

Rajkumar
http:#34135492
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now