Set command to concatteneate strings

  • I am trying to use a variabe to camcattenate a string

    I use following:

    DECLARE @MYVAR Char(1000) = 'INITIAL'

    if Conditon 1

    SET @MYVAR = @MYVAR + ' ABC'

    If Condition 2

    SET @MYVAR = @MYVAR + 'DEF'

    But Myvar nver changes from the Initial value

  • gerard-593414 (8/5/2012)


    I am trying to use a variabe to camcattenate a string

    I use following:

    DECLARE @MYVAR Char(1000) = 'INITIAL'

    if Conditon 1

    SET @MYVAR = @MYVAR + ' ABC'

    If Condition 2

    SET @MYVAR = @MYVAR + 'DEF'

    But Myvar nver changes from the Initial value

    Nice trick!=)))

    try

    DECLARE @MYVAR varChar(1000) = 'INITIAL'

    When you declare variable like char(n) and set it, the first M symbols will be set ('INITIAL'-7 chars in your case), other 1000-7=993 symbols will be ' '.

    After that when you selecting variable in SSMS - it won't show you all the 1000 symbols, instead it will show you the several first one, and it will seem like 'INITIAL '.


    I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
    Blog: http://somewheresomehow.ru[/url]
    Twitter: @SomewereSomehow

  • Ok thanks for that.

    So each time I Rtrim the variable and it seems to work

  • Yes, because it trims all the right spaces. So maybe you should consider to change data type to varchar.

    Good luck!


    I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
    Blog: http://somewheresomehow.ru[/url]
    Twitter: @SomewereSomehow

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

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