Question : MS SQL 2005, split string.

Hi, I have a column with a value like this:     v1,v2,v3, v4, v5

And now I want to get data like this:
v1
v2
v3
v4
v5

How can I do it with select statement?

Answer : MS SQL 2005, split string.

First create the function below...
Then use this query:

>> select [Column] from [table]

Select X.one, a.*
From [table] a
outer apply dbo.multirow(a.Text) X

Note: You can actually use either of the two functions below, but since I'm giving a solution (outer apply) I thought I'd poke my version of the split (using CTE in SQL Server 2005).
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
if object_id('dbo.multiRow') is not null
drop function dbo.multiRow
GO
create function dbo.multiRow(@data varchar(max), @delims varchar(10))
returns table as return
with cte (one,rem)
as
(
select
	rtrim(ltrim(LEFT(@data, charindex(@delims,@data+@delims)-1))),
	substring(@data, charindex(@delims,@data+@delims)+LEN(@delims+'.')-1, LEN(@data))
where nullif(@data,'') is not null
union all
select
	rtrim(ltrim(LEFT(rem, charindex(@delims,rem+@delims)-1))),
	substring(rem, charindex(@delims,rem+@delims)+len(@delims+'.')-1, LEN(rem))
from cte
where nullif(rem,'') is not null
)
select one from cte where nullif(one,'') is not null
GO
Random Solutions  
 
programming4us programming4us