?
Solved

SQL Server - Case Statement resulting in NULL

Posted on 2011-10-03
2
Medium Priority
?
434 Views
Last Modified: 2012-05-12
I have a result set in my query that contains empty strings like ' '.  I would like to make those NULL in the select clause in my sql statement like this...

CASE WHEN Value = ' ' THEN NULL END AS Value

But when I do this I get this error...

None of the result expressions in a CASE specification can be NULL.

Is there a better way to accomplish what I'm after?

Thanks
0
Comment
Question by:JosephEricDavis
  • 2
2 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 2000 total points
ID: 36906002
try the NULLIF function

select NULLIF(Value,' ') as Value
from MyTable
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36906016
I suggest also trimming the value and testing for empty string instead:

select NULLIF( RTRIM(Value), '' ) as Value
from MyTable
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question