SQL Server - Case Statement resulting in NULL

Posted on 2011-10-03
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...


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?

Question by:JosephEricDavis
    LVL 33

    Accepted Solution

    try the NULLIF function

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

    Expert Comment

    I suggest also trimming the value and testing for empty string instead:

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
    Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now