SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to split a comma in a string
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

soori457
Yak Posting Veteran

India
85 Posts

Posted - 03/11/2008 :  08:32:27  Show Profile  Reply with Quote
I have a string

@string = 'abc,def,ghi'

Now I have to display

abc
def
ghi

I have to separate a comma in that
Is there an function like split in sql

plz help me
Thanks in Advance

Suresh Kumar

khtan
In (Som, Ni, Yak)

Singapore
17650 Posts

Posted - 03/11/2008 :  08:33:58  Show Profile  Reply with Quote
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648


KH
Time is always against us

Go to Top of Page

ranganath
Posting Yak Master

209 Posts

Posted - 03/11/2008 :  08:37:18  Show Profile  Reply with Quote
Hi,

Try with this

DECLARE @data NVARCHAR(MAX),
@delimiter NVARCHAR(5)
SELECT @data = '1,2,3,6,7,8,R,a,n,g,a,n,a,t,h',
@delimiter = ','
DECLARE @textXML XML;
SELECT @textXML = CAST('<d>' + REPLACE(@data, @delimiter, '</d><d>') + '</d>' AS XML);
SELECT @textXML
SELECT T.split.value('.', 'nvarchar(max)') AS data
FROM @textXML.nodes('/d') T (split)

it will work on Sql2005 only

(or)

DEclare @s Varchar(100) , @Q varchar(1000)
Set @s = '1,2,3,4,5,6,a,s,d,f'
Set @Q = Replace(@s, ',','')
SELECT SUBSTRING ( @Q, number+1,1) AS OrderID FROM
Master..spt_Values n where n.Number < DataLength(@Q) and type = 'P'

Edited by - ranganath on 03/11/2008 09:23:13
Go to Top of Page

soori457
Yak Posting Veteran

India
85 Posts

Posted - 03/11/2008 :  08:52:39  Show Profile  Reply with Quote
Thanks for ur reply Mr.Ranganath

Can u explain me, why we need xml there

Suresh Kumar
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 03/11/2008 :  09:26:19  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Ranganaths second suggestion will only work with single digit items, and not per as OP requested in orignal post.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 03/11/2008 :  20:38:33  Show Profile  Reply with Quote
DECLARE @csv VARCHAR(255)
SET @csv = 'gvfgmgm,f,a,cls,pcl,r,o,fhhh,h'
;


with s(start) as
(
SELECT distinct charindex(',',','+@csv+',',p)
FROM
(
select * from
(
select row_number() over (order by m1.number) p from master..spt_values m1,master..spt_values m2
) z
where p <=len(@csv)+2) x
),
chunks(chunk) as
(
select
substring(@csv,start,(select min(start) from s as s2 where s2.start>s.start)-start-1)
from s
where start<len(@csv)+2
)

select * from chunks

I'd fix up your parameter to pass something useful though
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000