Split based on the input

  • hi

    how to split the string based on the input

    say starting is 1 and next is also 1 the difference is 1

    say starting is 2 and next is also 1 the difference is 1

    say starting is 3 and next is also 5 the difference is 5

    declare @input varchar(1000)='1,1,5,5,6,2'

    start end,difference

    1,1,1

    2(start+difference),2,1

    3(start+difference),7,5

    8(start+difference),12,5

    13(start+difference),18,6

    19(start+difference),20,2

    Thanks!

  • hi,

    declare @input varchar(1000)='1,1,5,5,6,2'

    start, difference

    1,1

    2,1

    3,5

    8,5

    13,6

    19,2

    Go

    declare @input varchar(1000)='5,3,2'

    start, difference

    1,5

    6,3

    9,2

  • I got it thanks!!

    Create Function [dbo].[Splited_Values]

    (

    @CommaSeparatedString varchar(8000)

    )

    RETURNS @Table Table ( startid int,ID int)

    Begin

    Declare @TempStr varchar(8000) ,@tempval int=1

    Set @TempStr = @CommaSeparatedString + ','

    While Len(@TempStr) > 0

    Begin

    Insert Into @Table Select @tempval,SubString(@TempStr,1,CharIndex(',',@TempStr)-1)

    Set @tempval= @tempval+SubString(@TempStr,1,CharIndex(',',@TempStr)-1)

    Set @TempStr = Right(@TempStr,Len(@TempStr)-CharIndex(',',@TempStr))

    End

    Return

    End

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply