handling Nulls in case series

  • Why is the "is null" throwing a syntax error?:

    UPDATE [GCDF_DB].[dbo].[People]

    SET [educationtext] =

    CASE [education]when 1 then 'Post Graduate'

    when 2 then '4 Year Degree'

    when 3 then 'Associates Degree'

    when 4 then 'High School'

    when is null then 'Unknown'END

     
  • You can't use IS NULL in a CASE expression using the CASE {value} WHEN {expression} format; you must use the CASE WHEN {value} = {expression} style syntax:

    USE GCDF_DB;
    UPDATE [dbo].[People]
    SET [educationtext] = CASE WHEN [education] = 1 THEN 'Post Graduate'
    WHEN [education] = 2 THEN '4 Year Degree'
    WHEN [education] = 3 THEN 'Associates Degree'
    WHEN [education] = 4 THEN 'High School'
    WHEN [education] IS NULL THEN 'Unknown'
    END;

     

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Presumably because NULL is not a value.

    Have you considered just using ELSE?

    Otherwise, use the extended 'Searched CASE' syntax:

    ... WHEN Education is NULL THEN 'Unknown'

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • UPDATE [GCDF_DB].[dbo].[People]

    SET [educationtext] =

    CASE [education]when 1 then 'Post Graduate'

    when 2 then '4 Year Degree'

    when 3 then 'Associates Degree'

    when 4 then 'High School'

    ELSE 'Unknown'END

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply