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

pushp82
Yak Posting Veteran

83 Posts

Posted - 09/07/2013 :  01:46:06  Show Profile  Reply with Quote
Guys,

A#B#C
A#B
A#B#C#D
How do I split above value in columns by "#"?
I need output:

Column1 Column2 Column3 Column4
A B C
A B
A B C D


Please Help!


Edited by - pushp82 on 09/07/2013 01:49:02

bitsmed
Constraint Violating Yak Guru

405 Posts

Posted - 09/07/2013 :  03:11:32  Show Profile  Reply with Quote
This might work for you, but be aware - this will not work when field contains < or >

with yourtable (yourfield)
  as (select 'A#B#C' as yourfield
      union all
      select 'A#B' as yourfield
      union all
      select 'A#B#C#D' as yourfield
     )
select convert(xml,'<f>'+replace(yourfield,'#','</f><f>')+'</f>').value('/f[1]','varchar(10)') as col1
      ,convert(xml,'<f>'+replace(yourfield,'#','</f><f>')+'</f>').value('/f[2]','varchar(10)') as col2
      ,convert(xml,'<f>'+replace(yourfield,'#','</f><f>')+'</f>').value('/f[3]','varchar(10)') as col3
      ,convert(xml,'<f>'+replace(yourfield,'#','</f><f>')+'</f>').value('/f[4]','varchar(10)') as col4
  from yourtable
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17642 Posts

Posted - 09/07/2013 :  03:15:15  Show Profile  Reply with Quote
use fnParseString from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033


KH
Time is always against us

Go to Top of Page

pushp82
Yak Posting Veteran

83 Posts

Posted - 09/07/2013 :  03:24:04  Show Profile  Reply with Quote
I did this a bit old way...

DECLARE @mnu varchar(max)
DECLARE @strXML VARCHAR(max)
DECLARE @XML XML

SET @strXML = '<table><row><col>' +replace((REPLACE(@mnu,'#','</col><col>')),';','</col></row></table>')

set @XML=cast(@strXML as XML)
insert into table_1
SELECT @mnu_cat,line.col.value('col[1]', 'varchar(1000)') AS col1
,line.col.value('col[2]', 'varchar(1000)') AS col2
,line.col.value('col[3]', 'varchar(1000)') AS col3

FROM @XML.nodes('/table/row') AS line(col)

and a loop over it to cover one by one

Thanks for the responce 'bitsmed' and 'khtan'....

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 09/08/2013 :  04:17:59  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Also refer this http://beyondrelational.com/modules/2/blogs/70/posts/10844/splitting-delimited-data-to-columns-set-based-approach.aspx

Madhivanan

Failing to plan is Planning to fail
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.45 seconds. Powered By: Snitz Forums 2000