Case When Resulting in Null

  • Hi All,

    I just need clarification of why a field for some records would result in a null value for a query like the following, particularly for the StatusCd field. I believe it would be when none of the whens apply? Please advise:

    SELECT InternalICN

    , EncounterLineNum

    , STSDate StatusDt

    , case HeaderStatus

    when 'F0' then 3 -- PAY

    when 'VE' then 3

    when 'F2' then 4 -- DENY

    when 'P2' then 9 -- SUSPEND

    when 'PC' then 10

    end AS StatusCd

    , case HeaderStatus

    when 'VE' then 1500

    else 600

    end AS StatusReasonCd

    , isnull(drec.dpwicn,'') as DPWICN

    FROM T277.Enc277_2200D drec join

    T277.EncounterTrackingTemp et on et.InternalICN = drec.TraceNo

    where et.StatusCd <> 2

    Thanks!

  • you are correct; you are testing for five specific values in your CASE, and there's no explicit ELSE in it to handle other values, and the built in logic structure of the case returns NULL for items that don't match the tests in the CASE when no explicit ELSE is provided.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks, Lowell. Your response was helpful...I know it's something that may have been obvious, but I just wanted to verify. 🙂

  • Sanity Checks are always a good thing! glad i could help!

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

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

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