Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Parsing / breaking character separated values in a table in one query

By Atif-ullah Sheikh,

There might be number of ways to accomplish this task but getting the result with a single query is sort of a challenge. I will explain to accomplish this task with an example.Consider a table having following data,

 

Declare @vTable Table(id int,val varchar(100))
Insert into @vTable Values (1,'Atif Sheikh2 Sheikh123')
Insert into @vTable Values (2,'Asif Sheikh1 Sheikh2 Sheikh3')

 

Now if you want to break the space separated values in the above table as;

ID Val

1 Atif

1 Sheikh2

1 Sheikh123

2 Asif

2 Sheikh1

2 Sheikh2

2 Sheikh3

 

the query is stated in the code.

 

Let me explain the above mentioned query. As you can see that i have used a recursive function CTE.The first query in the CTE query is

;Select id,substring(val,0,case when charindex(' ',val,0) = 0 then Len(val) else charindex(' ',val,0) + 1 end) as val,charindex(' ',val,0) + 1 strpos 
from @vTable

This creates the master record for parsing the string. This loads the first value in the required data set and to use in the second part. Here I used strpos column to get the position to parse the next value in the character separated string. It is just the length of the previous parsed value.

The second part is;

Select b.id,substring(b.val,strpos,case when charindex(' ',b.val,strpos) = 0 then Len(b.val) else charindex(' ',b.val,strpos) - strpos end) as val,charindex(' ',b.val,strpos) + 1 strpos 
from @vTable b
Inner Join wcte on wcte.id = b.ID
and wcte.strpos <> 1

 

This query uses the posintion in strpos column and parses the next value. And as it is recursive, it goes on until the strpos > 1. strpos, as you can see in the first part of the query, is the charindex of the character which is used to separate the values in the string.

That is it...

Total article views: 2089 | Views in the last 30 days: 3
 
Related Articles
FORUM

vtable

All, I have faced a different problem recently. When i executed a procedure from back end, it ...

FORUM

charindex or like condition

Hi iam executing the below query which is not executing even for hours also if i replcae like instea...

FORUM

Searching for URL contents using CHARINDEX

Providing CHARINDEX parameter from table

SCRIPT

Extend CHARINDEX with occurance matching

Retrieve the charindex of matching string for the desired occurrence

FORUM

problems using charindex function

charindex does not work they way i think it should work :)

Tags
script    
sql server 2005    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones