Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 splite a string

Author  Topic 

tarz
Starting Member

31 Posts

Posted - 2008-03-04 : 10:45:29
Hi I am trying to split a string and display its values.
ie.
I have SET @c='7890A098908A90809A22'

output should look like:
VALUE1 VALUE2 VALUE3 VALUE4
7890 098908 90809 22

Please advice,
Thank you

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2008-03-04 : 10:49:13
look up substring and charindex.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-03-04 : 10:49:54
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-03-04 : 11:09:22
declare @c varchar(100)
SET @c='7890A098908A90809A22'
select
parsename(replace(@c,'A','.'),4),
parsename(replace(@c,'A','.'),3),
parsename(replace(@c,'A','.'),2),
parsename(replace(@c,'A','.'),1)

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-03-04 : 11:16:57
Keep in mind that you can apply PARSENAME only if you're sure you have a maximum of 4 values to be extracted to four different fields. If your string contains more than 4 values seperated by A then parsename wont work.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-03-04 : 11:33:40
quote:
Originally posted by visakh16

Keep in mind that you can apply PARSENAME only if you're sure you have a maximum of 4 values to be extracted to four different fields. If your string contains more than 4 values seperated by A then parsename wont work.


Yes it is.

For any number of columns


declare @s varchar(8000)
select @s='7890A098908A90809A22'
select @s=''''+replace(@s,'A',''',''')+''''
exec('select '+@s)

Madhivanan

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

tarz
Starting Member

31 Posts

Posted - 2008-03-04 : 11:57:44
Thank you all for your help,

Is there away to add column-names as well to the output?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-03-05 : 03:18:20
quote:
Originally posted by tarz

Thank you all for your help,

Is there away to add column-names as well to the output?



Where do you want to show data?

Madhivanan

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

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-03-05 : 22:27:50
quote:
Originally posted by khtan

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033


KH
[spoiler]Time is always against us[/spoiler]





What does a split on delimiters have to do with this?

--Jeff Moden
Go to Top of Page
   

- Advertisement -