August 20, 2009 at 10:31 am
D/A,
Need to find out the position of a charcter's in a string
Eg. (Position of "1" in a given string)
String : '000100010001'
Result should be : 4,8,12
Pls guide/help how to get the result set
Thanks....Gugan
August 20, 2009 at 10:43 am
declare @input varchar(100)
set @input = '000100010001'
;with tally (N) as (select row_number() over (order by id) from master..syscolumns)
select N from tally
where substring(@input,N,1) = '1'
and N <= len(@input)
edited to add:
Whups, sorry Seth. Didn't realize you had already posted.
That's a good article by Jeff and well worth reading.
To get 4,8,12 as one character string, use this.
--
declare @input varchar(100)
set @input = '000100010001'
--
;with tally (N) as (select row_number() over (order by id) from master..syscolumns)
--
,positions (N,Nstr) as
(select N,cast(N as varchar(5)) from tally
where substring(@input,N,1) = '1'
and N <= len(@input)
)
--
select stuff(( SELECT ',' + Nstr
FROM positions
ORDER BY N
FOR XML PATH('')
) ,1,1,'')
--
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply