output as per requirement in sql code -


input: '1,2,4,5,c1,c2,c5,c7,c8,c9,t1,t5,t6,t10'

output: 1-2,4-5,c1-c2,c5,c7-c9,t1,t5-t6,t10

here when give input like, comma separated string of different series output should each series hyphen , separated comma output should not in table format.

--exec sequenceseriesreplacebyhyphen '1,2,3,11,12,13,15,16,20,21,22,c 1,c 2,c 3,c 5,c 6,c 9,72,73,t 1,t 2,t 5,t 99,55,56,57,60,61,v6,v7,v9,99,100,101'    alter procedure dbo.sequenceseriesreplacebyhyphen @strinputlist varchar(max)-- inputstring  begin      create table dbo.#tempchar(value varchar(10))     insert #tempchar     select value dbo.split2(@strinputlist) --where value '%[^0-9]%';      ;with ctesplit(letter, number)       as(      select ltrim(rtrim(left(value, patindex('%[0-9]%', value) -1))),                cast(right(value, len(value) - patindex('%[0-9]%', value) + 1) int)      #tempchar        )            ,cte (                     select *,rn = number - row_number() over(partition letter order number)                     ctesplit                     )             ,ctefinal as(                          select letter,                                 startnumber = min(number),                                 endnumber = max(number)                            cte                          group letter, rn)      select stuff((select ',' + letter + '' + cast(startnumber varchar(max)) +                                               case                                               when startnumber = endnumber ''                                               else '-' + letter + '' + cast(endnumber varchar(max))                                               end      ctefinal      order letter, startnumber, endnumber      xml path('')),1, 1, '') result  end 

alter function [dbo].[split2](@input varchar(max) ) returns  @result table(value varchar(max)) begin       declare @str varchar(20)       declare @ind int       if(@input not null)       begin             set @ind = charindex(',',@input)             while @ind > 0             begin                   set @str = substring(@input,1,@ind-1)                   set @input = substring(@input,@ind+1,len(@input)-@ind)                   insert @result values (@str)                   set @ind = charindex(',',@input)             end             set @str = @input             insert @result values (@str)       end       return end                   

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -