CONCAT'ing NULL fields

前端 未结 11 844
小鲜肉
小鲜肉 2021-02-01 00:25

I have a table with three fields, FirstName, LastName and Email.

Here\'s some dummy data:

FirstName | LastName | Email
Adam        West       adam@west.c         


        
相关标签:
11条回答
  • 2021-02-01 01:11

    Stefan's answer is correct. To probe a little bit deeper you need to know that NULL is not the same as Nothing. Null represents the absence of a value, or in other words, not defined. Nothing represents an empty string which IS in fact a value.

    Undefined + anything = undefined

    Good database tidbit to hold onto!

    0 讨论(0)
  • 2021-02-01 01:14

    Try

    ISNULL(FirstName, '<BlankValue>') -- In SQL Server
    IFNULL(Firstname, '<BlankValue>') -- In MySQL
    

    So,

    CONCAT(ISNULL(FirstName,''),ISNULL(LastName,''),ISNULL(Email,'')) -- In SQL Server
    CONCAT(IFNULL(FirstName,''),IFNULL(LastName,''),IFNULL(Email,'')) -- In MySQL
    

    would return the same thing without the null issue (and a blank string where nulls should be).

    0 讨论(0)
  • 2021-02-01 01:16

    In mysql isnull wont work some time. try IFNULL(),

    CONCAT(IFNULL(FirstName,''),IFNULL(LastName,''),IFNULL(Email,''))
    
    0 讨论(0)
  • 2021-02-01 01:17

    Starting from MS SQL Server 2012 it was introduced CONCAT function and according to MSDN

    Null values are implicitly converted to an empty string. If all the arguments are null, an empty string of type varchar(1) is returned.

    so it's enough to use CONCAT without IsNull

    CONCAT(FirstName, LastName, Email)
    
    0 讨论(0)
  • 2021-02-01 01:19

    If you get (like I do in MySQL):

    #1582 - Incorrect parameter count in the call to native function 'ISNULL'
    

    You can replace ISNULL function by COALESCE:

    CONCAT(COALESCE(FirstName,''),COALESCE(LastName,''),COALESCE(Email,''))
    
    0 讨论(0)
提交回复
热议问题