Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Set command to concatteneate strings Expand / Collapse
Author
Message
Posted Sunday, August 5, 2012 3:03 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 18, 2014 8:36 AM
Points: 23, Visits: 36
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
Post #1340254
Posted Sunday, August 5, 2012 6:57 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, November 14, 2014 9:53 PM
Points: 75, Visits: 444
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
Twitter: @SomewereSomehow
Post #1340262
Posted Sunday, August 5, 2012 7:59 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 18, 2014 8:36 AM
Points: 23, Visits: 36
Ok thanks for that.
So each time I Rtrim the variable and it seems to work
Post #1340272
Posted Sunday, August 5, 2012 8:16 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, November 14, 2014 9:53 PM
Points: 75, Visits: 444
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
Twitter: @SomewereSomehow
Post #1340275
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse