Avatar of Lisa Callahan
Lisa CallahanFlag for United States of America

asked on 

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!
Microsoft SQL Server

Avatar of undefined
Last Comment
_agx_
Avatar of _agx_
_agx_
Flag of United States of America image

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>' 
            END

Open in new window


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

        <ParameterValues><ParameterValue><Name>SPLC</Name><Value></ParameterValue>
Avatar of Lisa Callahan
Lisa Callahan
Flag of United States of America image

ASKER

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:

<ParameterValues><ParameterValue><Name>SPLC</Name></ParameterValue></ParameterValues>
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Lisa Callahan
Lisa Callahan
Flag of United States of America image

ASKER

YES!

This is exactly what I am after. Thank you for the very helpful explanation as well. I am always trying to improve my coding :)
Avatar of _agx_
_agx_
Flag of United States of America image

You're welcome. Glad it helped :)
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo