264

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
  • 3
    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

8 Answers 8

419

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
Sign up to request clarification or add additional context in comments.

7 Comments

You can use if select CONCAT(if(affiliate_name is null ,'',affiliate_name),'- ',if(model is null ,'',affiliate_name)) as model from devices
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.
mysql has IFNULL(arg, default) instead COALESCE with the same syntax
not work for me , still return null value when join column A = 'data person' with column B = null value . anyone know why ?
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.
|
176

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 Comments

Sorry neeraj i missed the '_' between Concat and WS Please try again with CONCAT_WS().I have updated the answer please check,
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.
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
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.
21

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 Comments

obviously, because the first field is the string that it will concatenate with (WS = with string)
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)
16

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)

Comments

13

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

Comments

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

1 Comment

in mysql IFNULL() instead of ISNULL()
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

Comments

0

An important TIP: Variables should have DIFFERENT NAMES from columns or it can cause failure.

DECLARE CAMPO    ...... SELECT CAMPO.... FETCH INTO CAMPO = FAILURE
DECLARE mycampo  .......SELECT CAMPO...  FETCH INTO mycampo = SUCCESS

Info:  https://bugs.mysql.com/bug.php?id=20834

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.