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
 How to split columns into multiple rows

Author  Topic 

awalsql
Starting Member

11 Posts

Posted - 2008-06-07 : 20:07:45
SOURCE TABLE
ID DESCRIPTION
1 I am a programmer
2 I am a doctor

Destination Table

ID LINE DESCRIPTION(Varchar10)
1 1 I am a pro
1 2 grammer
2 1 i am a doc
2 2 tor


Please someone help me on this.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-08 : 05:08:51
[code]SELECT s.ID,
SUBSTRING(s.DESCRIPTION,v.number * 10+1,10)
FROM SourceTable s
CROSS JOIN master..spt_values v
WHERE v.type'p'
AND SUBSTRING(s.DESCRIPTION,v.number * 10+1,10) IS NOT NULL[/code]
Go to Top of Page

awalsql
Starting Member

11 Posts

Posted - 2008-06-08 : 11:40:13
visakh16,
That was brilliant!! Thanks a lot.
Go to Top of Page

awalsql
Starting Member

11 Posts

Posted - 2008-06-08 : 12:13:53
But how do I get this line number?
ID LINE DESCRIPTION(Varchar10)
1 1 I am a pro
1 2 grammer
2 1 i am a doc
2 2 tor

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-08 : 13:21:00
OOps sorry missed that

SELECT s.ID,
v.number+1 as Line,
SUBSTRING(s.DESCRIPTION,v.number * 10+1,10)
FROM SourceTable s
CROSS JOIN master..spt_values v
WHERE v.type='p'
AND SUBSTRING(s.DESCRIPTION,v.number * 10+1,10) IS NOT NULL
Go to Top of Page
   

- Advertisement -