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.
| Author |
Topic |
|
ram_kri
Starting Member
2 Posts |
Posted - 2007-07-05 : 07:16:01
|
| Hi,I have this following situation and how to create a query for this :I have a table:Desc----Columns : ID, DESCRIPTIONLet's say we have 3 rows in the table :1 abc,def,ghi2 jklmn,opq,rrsstuvew3 xyz,aaaa,bbbbbbbbb,ddddddd,eeeeeeNow out of that table I want to create a table that has :NewDesc-------Columns : ID, DescriptionSplited1 abc1 def1 ghi2 jklmn2 opq2 rrsstuvew3 xyz3 aaaa3 bbbbbbbbb3 ddddddd3 eeeeeeI dont mind if we have to insert/update the exisiting table or move it into a new table.Idea is to retain the id, but split the description based on coma and put it in a different row.So that there wont be comas anywhere in the table.I have written a split function for the same :as below ://///////////////////////////////////////////////////////set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER FUNCTION [dbo].[Split]( @List nvarchar(2000), @SplitOn nvarchar(5)) RETURNS @RtnValue table ( Id int identity(1,1), Value nvarchar(100)) AS BEGINWhile (Charindex(@SplitOn,@List)>0)Begin Insert Into @RtnValue (value)Select Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1))) Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))End Insert Into @RtnValue (Value) Select Value = ltrim(rtrim(@List)) ReturnEND/////////////////////////////////////////////////////////////////////Now using this I want to write a query to achieve the above task.Any help in this regard would be great.Thanks in advance.Regards,Kris |
|
|
Kristen
Test
22859 Posts |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-05 : 07:19:56
|
use CROSS APPLYselect t.ID, s.Valuefrom table t cross apply split(t.DESCRIPTION) s KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ram_kri
Starting Member
2 Posts |
Posted - 2007-07-05 : 08:04:51
|
| Maaaaaan 'Khtan" !!!You rock !!It worked..never thought it would this simple.Thank you. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-05 : 08:10:09
|
If you are not using SQL Server 2005, it will not be that simple  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|