简体   繁体   中英

CASE Statement using IS NULL and IS NOT NULL

I'm building a new SQL table and I'm having some trouble with a CASE statement that I can't seem to get my head around.

I have two different clauses to be met within the WHEN parameter, separated by an AND clause. Both of these clauses are looking for NULL values, either being null or not null and variations of this.

      ,CASE WHEN [FULL_TELEPHONE_NUMBER] IS NOT NULL AND [EMAIL] IS NOT NULL THEN 'Phone Number AND Email Address Available'
        WHEN [FULL_TELEPHONE_NUMBER] IS NOT NULL AND [EMAIL] IS NULL THEN 'Phone Number ONLY Available'
        WHEN [FULL_TELEPHONE_NUMBER] IS NULL AND [EMAIL] IS NOT NULL THEN 'Email Address ONLY Available'
        WHEN [FULL_TELEPHONE_NUMBER] IS NULL AND [EMAIL] IS NULL THEN 'No Contact Details Available'
    ELSE 'ERROR' END AS '[CONTACT DETAILS AVAILABLE]'

Unfortunately, the data returns with only the first condition being printed for every result, regards as to what the queried columns contain.

Although the data held in the columns appears empty, is there a chance they may not be truly NULL? Or is NULL the correct term here, is there something better I could use?

Many thanks.

It's quite possible that the data is actually an empty string instead of null. You could use the following to cover for that:

WHEN IsNull([FULL_TELEPHONE_NUMBER], '') <> '' AND isnull([EMAIL], '') <> '' THEN 'Phone Number AND Email Address Available'

Here is another way to tackle this. You can use > '' instead of all those IS NULL and such.

CASE WHEN [FULL_TELEPHONE_NUMBER] + [EMAIL] > '' THEN 'Phone Number AND Email Address Available'
        WHEN [FULL_TELEPHONE_NUMBER] > '' AND [EMAIL] IS NULL THEN 'Phone Number ONLY Available'
        WHEN [FULL_TELEPHONE_NUMBER] IS NULL AND [EMAIL] > '' THEN 'Email Address ONLY Available'
        WHEN [FULL_TELEPHONE_NUMBER] IS NULL AND [EMAIL] IS NULL THEN 'No Contact Details Available'
    ELSE 'ERROR' END AS '[CONTACT DETAILS AVAILABLE]'

You can use nullif(col,'') to return null when the value of the first parameter is equal to the second parameter.

,CASE 
  WHEN nullif([FULL_TELEPHONE_NUMBER],'') IS NOT NULL AND nullif([EMAIL],'') IS NOT NULL 
    THEN 'Phone Number AND Email Address Available'
  WHEN nullif([FULL_TELEPHONE_NUMBER],'') IS NOT NULL AND nullif([EMAIL],'') IS NULL 
    THEN 'Phone Number ONLY Available'
  WHEN nullif([FULL_TELEPHONE_NUMBER],'') IS NULL AND nullif([EMAIL],'') IS NOT NULL 
    THEN 'Email Address ONLY Available'
  WHEN nullif([FULL_TELEPHONE_NUMBER],'') IS NULL AND nullif([EMAIL],'') IS NULL 
    THEN 'No Contact Details Available'
    ELSE 'ERROR' 
    END 
    AS '[CONTACT DETAILS AVAILABLE]'

Can also be simplified to:

, [contact details available] = case 
    when nullif([full_telephone_number],'') is not null 
     and nullif([email],'') is not null 
      then 'phone number and email address available'
    when nullif([full_telephone_number],'') is not null 
      then 'phone number only available'
    when nullif([email],'') is not null 
      then 'email address only available'
      else 'no contact details available'
      end 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM