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
 parsing the columns data

Author  Topic 

anjali5
Posting Yak Master

121 Posts

Posted - 2012-05-18 : 19:12:48
Hi All,

I have the data below in col1


col1
AB:5
AB:9 EF:5
EF:180
CD:12 AB:12


I want top parse it as below.


Col1 AB CD EF
AB:5 5
AB:9 EF:5 9 5
EF:180 180
CD:12 AB:12 12 12



How can I acheive this

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-18 : 22:59:25
[code]
select col1,
case when col1 like '%AB%'
then substring(col1, charindex('AB', col1) + 3, charindex(' ', col1 + ' ', charindex('AB', col1)) - charindex('AB', col1) - 3)
end,
case when col1 like '%CD%'
then substring(col1, charindex('CD', col1) + 3, charindex(' ', col1 + ' ', charindex('CD', col1)) - charindex('CD', col1) - 3)
end,
case when col1 like '%EF%'
then substring(col1, charindex('EF', col1) + 3, charindex(' ', col1 + ' ', charindex('EF', col1)) - charindex('EF', col1) - 3)
end
from tbl
[/code]


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

Go to Top of Page

anjali5
Posting Yak Master

121 Posts

Posted - 2012-06-28 : 13:54:42
Thanks!!
Go to Top of Page
   

- Advertisement -