255

I have following data in my table "devices"

affiliate_name  affiliate_location  model     ip             os_type    os_version 

cs1             inter               Dell     10.125.103.25   Linux      Fedora  
cs2             inter               Dell     10.125.103.26   Linux      Fedora  
cs3             inter               Dell     10.125.103.27   NULL       NULL    
cs4             inter               Dell     10.125.103.28   NULL       NULL    

I executed below query

SELECT CONCAT(`affiliate_name`,'-',`model`,'-',`ip`,'-',`os_type`,'-',`os_version`) AS device_name
FROM devices

It returns result given below

cs1-Dell-10.125.103.25-Linux-Fedora
cs2-Dell-10.125.103.26-Linux-Fedora
(NULL)
(NULL)

How to come out of this so that it should ignore NULL AND result should be

cs1-Dell-10.125.103.25-Linux-Fedora
cs2-Dell-10.125.103.26-Linux-Fedora
cs3-Dell-10.125.103.27-
cs4-Dell-10.125.103.28-
1
  • 2
    Just ran into this as well. I would consider near to insane that this is the default action. The exact opposite of programming with the least amount of surprises. Commented Aug 24, 2021 at 12:24

7 Answers 7

408

convert the NULL values with empty string by wrapping it in COALESCE

SELECT CONCAT(COALESCE(`affiliate_name`,''),'-',COALESCE(`model`,''),'-',COALESCE(`ip`,''),'-',COALESCE(`os_type`,''),'-',COALESCE(`os_version`,'')) AS device_name
FROM devices
7
  • 1
    You can use if select CONCAT(if(affiliate_name is null ,'',affiliate_name),'- ',if(model is null ,'',affiliate_name)) as model from devices Commented Sep 24, 2015 at 13:41
  • 11
    For those wonder, as I did, what the COALESCE function does: it returns the first non-NULL value parameter passed to it (or NULL if all parameters are NULL). By passing an empty string as the second parameter, you are guaranteeing it will not return NULL.
    – Jo.
    Commented Mar 10, 2017 at 17:03
  • 9
    mysql has IFNULL(arg, default) instead COALESCE with the same syntax Commented Feb 19, 2019 at 17:58
  • not work for me , still return null value when join column A = 'data person' with column B = null value . anyone know why ? Commented Jan 20, 2023 at 0:08
  • ah we cant use COALESCE(column, null) , i copied your code, and now its work . but for mysql 8 , we just use COALESCE(column, '') if we use ''` parenthesis , its return unknow column, and if we use null after define column, its return null if some value is null. Commented Jan 20, 2023 at 0:15
170

Use CONCAT_WS instead:

CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

SELECT CONCAT_WS('-',`affiliate_name`,`model`,`ip`,`os_type`,`os_version`) AS device_name FROM devices
4
  • Sorry neeraj i missed the '_' between Concat and WS Please try again with CONCAT_WS().I have updated the answer please check,
    – Gurmeet
    Commented Apr 1, 2013 at 10:14
  • 12
    Note that this solution hides a complete "column" (including the separator) if one of the middle fields is NULL. So this answer is only correct assuming that only the last field(s) can be NULL. Depending on your need, the COALEASE() answer below might be better.
    – Jannes
    Commented Nov 11, 2014 at 14:54
  • 1
    This only works if you want every member separated by the same separator. CONCAT doesn't have this limitation. I posted the solution as an answer here
    – patrick
    Commented Jan 5, 2015 at 13:05
  • 2
    Dangerous: SELECT CONCAT_WS(';',1,NULL) is identical with SELECT CONCAT_WS(';',NULL,1). Both gives 1. This is a problem if you want 1; respectively ;1.
    – giordano
    Commented Oct 21, 2022 at 17:17
22

CONCAT_WS still produces null for me if the first field is Null. I solved this by adding a zero length string at the beginning as in

CONCAT_WS("",`affiliate_name`,'-',`model`,'-',`ip`,'-',`os_type`,'-',`os_version`)

however

CONCAT("",`affiliate_name`,'-',`model`,'-',`ip`,'-',`os_type`,'-',`os_version`) 

produces Null when the first field is Null.

2
  • obviously, because the first field is the string that it will concatenate with (WS = with string) Commented Feb 7, 2015 at 1:17
  • 6
    CONCAT_WS is short for Concatenate With Separator. The first parameter is the separator and cannot be null. This is probably what you want instead: CONCAT_WS("-", affiliate_name, model, ip, os_type, os_version)
    – encrest
    Commented Dec 3, 2015 at 2:10
15

To have the same flexibility in CONCAT_WS as in CONCAT (if you don't want the same separator between every member for instance) use the following:

SELECT CONCAT_WS("",affiliate_name,':',model,'-',ip,... etc)
0
12
SELECT CONCAT(isnull(`affiliate_name`,''),'-',isnull(`model`,''),'-',isnull(`ip`,''),'-',isnull(`os_type`,''),'-',isnull(`os_version`,'')) AS device_name
FROM devices
1
12

Reason:

MySQL :: Reference Manual :: 12.8 String Functions and Operators says:

CONCAT() returns NULL if any argument is NULL.

Solution:

MySQL :: Reference Manual :: 12.5 Flow Control Functions says:

IFNULL(expr1,expr2) 

If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.

SELECT
    CONCAT(
        IFNULL(`affiliate_name`, ''),
        '-',
        IFNULL(`model`, ''),
        '-',
        IFNULL(`ip`, ''),
        '-',
        IFNULL(`os_type`, ''),
        '-',
        IFNULL(`os_version`, '')
    ) AS device_name
FROM
    devices
3

you can use if statement like below

select CONCAT(if(affiliate_name is null ,'',affiliate_name),'- ',if(model is null ,'',affiliate_name)) as model from devices

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.