Concatenation Logic

  • Hi All,

    I need to concatenate some code.For example--

    given input :123

    output: reg0000123.

    if input :21

    output :reg0000021

    repalcing 21 to last 2 zeros in string'regooooooo'.

    Can anyone tell me how to make the psuedo logic to achieve results in above format.Can we use LEN or STUFF functions.

    Thanks,

    Hemanth.

  • There are a number of ways of doing this sort of thing. An example is below

    [font="Courier New"]

    declare @string varchar(11)

    set @string='reg0000000'

    declare @input varchar(7)

    set @input='123'

    declare @output varchar(11)

    set @output=left(@string,len(@string)-len(@input))+@input

    print @output

    [/font]

    This performs string manipulation by finding the length of the @input string and using that to get the first characters in @string (by subtracting the length of @input from the length of @string).

    It then adds @input onto it to build a new string @output.

    Other ways of doing it might be to build up the output string from scratch by using replicate or using stuff:

    STUFF (@string, len(@string)-len(@input)+1 , len(@input) , @input )

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

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