Dynamic SQL CASE STATEMENT in a text string

I have created some code that will replace values using one long string. The problem is that the syntax is different if there is a NULL value in the variable.

I have the following SQL:

SET     Parameters = '<ParameterValues><ParameterValue><Name>SPLC</Name><Value>' + @SPLC + '</Value></ParameterValue></ParameterValues>'

that I want to replace with a CASE STATEMENT for the @SPLC value:

CASE WHEN @SPLC is null THEN '</ParameterValue>' ELSE '<Value>' + @SPLC + '</Value></ParameterValue>'

so the new code would look like this:

SET     Parameters = '<ParameterValues><ParameterValue><Name>SPLC</Name><Value>' + CASE WHEN @SPLC is null THEN '</ParameterValue>' ELSE '<Value>' + @SPLC + '</Value></ParameterValue>' + '</ParameterValues>'

But I can't figure it out.

Thank you!
Lisa CallahanBusiness Intelligence AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You're missing an END for the CASE

SELECT '<ParameterValues><ParameterValue><Name>SPLC</Name><Value>' 
          + CASE  
              WHEN @SPLC is null THEN '</ParameterValue>' 
              ELSE '<Value>' + @SPLC + '</Value></ParameterValue>' + '</ParameterValues>' 

Open in new window

But that produces this, which isn't valid XML (if that's what you're after)

Lisa CallahanBusiness Intelligence AnalystAuthor Commented:
Thank you. That does get me a bit further but you are correct in that is not what I am after. I want the string to look like this if the value is not null:

<ParameterValues><ParameterValue><Name>SPLC</Name><Value>' + @SPLC + '</Value></ParameterValue></ParameterValues>

and this if the value is null:

(Updated description)

I don't think you need CASE for that.  A simple ISNULL or COALESCE should work. Try this SELECT as a test:

SELECT '<ParameterValues><ParameterValue><Name>SPLC</Name>' 
          + ISNULL('<Value>' + @SPLC+ '</Value>', '')
          + '</ParameterValue></ParameterValues>' 

Open in new window

When @SPLC is null, the resulting string is:

Otherwise, say @SPLC = "abc", then the resulting string is:

The key is this part.  When you concatenate strings with a  NULL, the result is NULL. So you can use ISNULL to return an empty string instead.

            ISNULL('<Value>' + @SPLC+ '</Value>', '')

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lisa CallahanBusiness Intelligence AnalystAuthor Commented:

This is exactly what I am after. Thank you for the very helpful explanation as well. I am always trying to improve my coding :)
You're welcome. Glad it helped :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.