T-SQL CASE Clause: How to specify WHEN NULL

后端 未结 15 1906
心在旅途
心在旅途 2020-11-28 05:42

I wrote a T-SQL Statement similar like this (the original one looks different but I want to give an easy example here):

SELECT first_name + 
    CASE last_na         


        
相关标签:
15条回答
  • 2020-11-28 05:42

    NULL does not equal anything. The case statement is basically saying when the value = NULL .. it will never hit.
    There are also several system stored procedures that are written incorrectly with your syntax. See sp_addpullsubscription_agent and sp_who2.
    Wish I knew how to notify Microsoft of those mistakes as I'm not able to change the system stored procs.

    0 讨论(0)
  • 2020-11-28 05:47

    Jason caught an error, so this works...

    Can anyone confirm the other platform versions?
    SQL Server:

    SELECT
    CASE LEN(ISNULL(last_name,'')) 
    WHEN 0 THEN '' 
    ELSE ' ' + last_name
    END AS newlastName
    

    MySQL:

    SELECT
    CASE LENGTH(IFNULL(last_name,'')) 
    WHEN 0 THEN '' 
    ELSE ' ' + last_name
    END AS newlastName
    

    Oracle:

    SELECT
    CASE LENGTH(NVL(last_name,'')) 
    WHEN 0 THEN '' 
    ELSE ' ' + last_name
    END AS newlastName
    
    0 讨论(0)
  • 2020-11-28 05:48

    You can use IsNull function

    select 
        isnull(rtrim(ltrim([FirstName]))+' ','') +
        isnull(rtrim(ltrim([SecondName]))+' ','') +
        isnull(rtrim(ltrim([Surname]))+' ','') +
        isnull(rtrim(ltrim([SecondSurname])),'')
    from TableDat
    

    if one column is null you would get an empty char

    Compatible with Microsoft SQL Server 2008+

    0 讨论(0)
  • 2020-11-28 05:49

    Given your query you can also do this:

    SELECT first_name + ' ' + ISNULL(last_name, '') AS Name FROM dbo.person
    
    0 讨论(0)
  • 2020-11-28 05:50

    The issue is that NULL is not considered to be equal to anything even not to itself, but the strange part is that is also not not equal to itself.

    Consider the following statements (which is BTW illegal in SQL Server T-SQL but is valid in My-SQL, however this is what ANSI defines for null, and can be verified even in SQL Server by using case statements etc.)

    SELECT NULL = NULL -- Results in NULL

    SELECT NULL <> NULL -- Results in NULL

    So there is no true/false answer to the question, instead the answer is also null.

    This has many implications, for example in

    1. CASE statements, in which any null value will always use the ELSE clause unless you use explicitly the WHEN IS NULL condition (NOT the WHEN NULL condition )
    2. String concatenation, as
      SELECT a + NULL -- Results in NULL
    3. In a WHERE IN or WHERE NOT IN clause, as if you want correct results make sure in the correlated sub-query to filter out any null values.

    One can override this behavior in SQL Server by specifying SET ANSI_NULLS OFF, however this is NOT recommended and should not be done as it can cause many issues, simply because deviation of the standard.

    (As a side note, in My-SQL there is an option to use a special operator <=> for null comparison.)

    In comparison, in general programming languages null is treated is a regular value and is equal to itself, however the is the NAN value which is also not equal to itself, but at least it returns 'false' when comparing it to itself, (and when checking for not equals different programming languages have different implementations).

    Note however that in the Basic languages (i.e. VB etc.) there is no 'null' keyword and instead one uses the 'Nothing' keyword, which cannot be used in direct comparison and instead one needs to use 'IS' as in SQL, however it is in fact equal to itself (when using indirect comparisons).

    0 讨论(0)
  • 2020-11-28 05:50
    CASE  
        WHEN last_name IS null THEN '' 
        ELSE ' ' + last_name 
    END
    
    0 讨论(0)
提交回复
热议问题