March 16, 2006 at 10:45 pm
Hi,
I need a solution how i separate the comma seperated value and get the result as mentioned above?
manokarnan
March 17, 2006 at 4:18 am
is this for a single excercise (like a data load) and won't do agin (hopefully) or is it something that you ned to build into a procedure for constant re-use?
MVDBA
March 17, 2006 at 4:57 am
If I had a penny for how many times this is requested...
--This SQL script is safe to run
declare @t table (id int identity(1, 1), v varchar(50))
insert @t
select 'M1,M2,M3,M4'
union all select 'M1,M2,M3'
union all select 'N1,N2,N3,N4'
union all select 'N10,N12'
declare @Numbers table (i int identity(1, 1), j bit)
insert into @Numbers
select top 50 null from master.dbo.syscolumns a, master.dbo.syscolumns b
select t.id, left(substring(','+v, i + 1, 50), charindex(',', substring(','+v+',', i + 1, 50))-1) as v
from @t t, @Numbers
where substring(','+v, i, 1) = ','
order by t.id, v
(...I would have £12.37)
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
March 17, 2006 at 5:07 am
or this script
http://www.sqlservercentral.com/scripts/viewscript.asp?scriptid=319
personally if it's data minipulation as a one off excel (spit!) has 2 functions - deleimeter to column - column to row - these can be used to quickly handball data for one off situations
MVDBA
March 17, 2006 at 5:58 am
Thanks for all you replies. It's not a single time query i want to use this concept inside the stored procedure.
ManoKarnan
October 28, 2010 at 9:07 am
Hi
I am having a situation like this
sl Field
1 Text1,Text5
2 Text1,Text2,Texte5
3 Text5
4 Text3, Text4, Text1
5 Text4
6 Text2, Text 3
and I want
Text1
Text2
Text3
Text4
Text5
Distinct from Comma seperated field
can some one please help me on this
Thanks and Regards
Selwyn
October 28, 2010 at 9:15 am
One option (of many) is to simply adapt the above (by adding a distinct and removing the ids):
declare @t table (id int identity(1, 1), v varchar(50))
insert @t
select 'M1,M2,M3,M4'
union all select 'M1,M2,M3'
union all select 'N1,N2,N3,N4'
union all select 'N10,N12'
declare @Numbers table (i int identity(1, 1), j bit)
insert into @Numbers
select top 50 null from master.dbo.syscolumns a, master.dbo.syscolumns b
select distinct left(substring(','+v, i + 1, 50), charindex(',', substring(','+v+',', i + 1, 50))-1) as v
from @t t, @Numbers
where substring(','+v, i, 1) = ','
order by v
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply