November 11, 2010 at 10:26 am
HI everyone,
I have a issue
If the input string = '1001,2001,1002,2002,1003,2003'
I need the result as
@id1 = '1001,1002,1003'
@id2 = '2001,2002,2003'
Any ideas
thankyou
November 11, 2010 at 10:46 am
declare @s-2 varchar(100) = '1001,2001,1002,2002,1003,2003';
declare @id1 varchar(100);
declare @id2 varchar(100);
set @id1 = STUFF((SELECT ',' + Item
FROM dbo.DelimitedSplit8K(@s, ',')
WHERE ItemNumber % 2 = 1 -- odd numbers
ORDER BY ItemNumber
FOR XML PATH(''), TYPE).value('.','varchar(max)'),1,1,'');
set @id2 = STUFF((SELECT ',' + Item
FROM dbo.DelimitedSplit8K(@s, ',')
WHERE ItemNumber % 2 = 0 -- even numbers
ORDER BY ItemNumber
FOR XML PATH(''), TYPE).value('.','varchar(max)'),1,1,'');
select @id1, @id2;
Click here for the latest Delimited Split Function.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 11, 2010 at 10:51 am
Beat me to it! Was about to post almost the same thing.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
November 11, 2010 at 11:23 am
Thanks alot WayneS n Gsquared
I will convert this as a function for my issue.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply