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
 split array
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

slimt_slimt
Constraint Violating Yak Guru

Slovenia
409 Posts

Posted - 02/21/2008 :  08:11:36  Show Profile  Reply with Quote
hi,

how can i split text separated by semicolumn in different cells:

text1;text2;text3;
into
1 - text1
2 - text2
3 - text3

thank you

madhivanan
Premature Yak Congratulator

India
20440 Posts

Posted - 02/21/2008 :  08:12:41  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Search for Split function here

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

LoztInSpace
Aged Yak Warrior

682 Posts

Posted - 02/21/2008 :  20:52:00  Show Profile  Reply with Quote
Here's yet another way I wrote recently. I kind of like it because there is no function to create so you can use it in situations when you can't modify the database.
Of course doing this stuff sucks - fix up your data model if you can.


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 number p from master..spt_values where type='p' and number<=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
)

/* 
Your query goes here.  
With the CTE you have access to a table called 'chunks' which has a column called 'chunk' 
*/

select * from blah 
where wibble in(
select 
chunk 
from chunks
)


Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
13562 Posts

Posted - 02/21/2008 :  20:56:11  Show Profile  Send khtan a Yahoo! Message  Reply with Quote
or if you are lazy to write one yourself just use one of the below
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=CSVTable
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648


KH
Time is always against us


Edited by - khtan on 02/21/2008 20:56:35
Go to Top of Page

LoztInSpace
Aged Yak Warrior

682 Posts

Posted - 02/21/2008 :  21:04:45  Show Profile  Reply with Quote
Just realised this is limited to strings of 2045 chars. This version will go further:


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
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 Version 3.4.03