Timmy
Timmy

Reputation: 553

Concatenate string using IF statement

Sorry if this is a duplicate, I feel like this has had to have been asked before, but I might just not know how to word to search correctly.

So, in my stored I take in some information and I want to create a varchar string based on this information. Lets say I have these three variables.

@String varchar(MAX) = '',
@BroughtInInfo bit

And now I have something like the following

SET @String = 'Here is a string and I want to add'
    IF @BroughtInInfo = 1
    BEGIN  
      +'This info'+
    END
    ELSE
      +'That info'+
'Then more stuff here after conditional statement'

Now, I'm getting syntax error near + I've tried a lot of combinations of moving the plus signs around the conditional statement but it doesn't play nice.

I'm pretty new to SQL so any tips and tricks will help! Thanks!

Upvotes: 0

Views: 1293

Answers (2)

Manohar Patil
Manohar Patil

Reputation: 39

Try This on SQLServer 2008 it will work

declare @String varchar(max);

declare @BroughtInInfo bit;

SET @String = 'Here is a string and I want to add'

IF @BroughtInInfo = 1
BEGIN  
SET @String +='This info'
END
ELSE
  SET @String +='This info'   select @String

Upvotes: 0

Tomalak
Tomalak

Reputation: 338416

The expression you are looking for is CASE (MSDN).

SET @String = 'Here is a string and I want to add' + 
    CASE WHEN @BroughtInInfo = 1 
        THEN 'This info'
        ELSE 'That info'
    END
    + 'Then more stuff here after conditional statement'

Upvotes: 1

Related Questions