25

If I have this - tadd is the Address table:

CONCAT(tadd.street_number, ' ',
            tadd.street_name,', ',
            tadd.apt_number,', ',
            tadd.city,', ',
            tadd.postal_code,', ',
            tadd.country) AS 'Address'

Is there a way to exclude the apt_number if it doesn't exist?

I was thinking of:

WHERE tadd.apt_number IS NOT NULL

But it will return only those rows with apt_number, and even if something works how do I then deal with that extra comma.

If it's a duplicate please post a link in comments.

oNare
  • 3,181
  • 2
  • 20
  • 35
ed-ta
  • 453
  • 2
  • 6
  • 10

4 Answers4

30

If you want to skip NULL values (but not empty strings), you can use CONCAT_WS() function:

CONCAT_WS( ', ',            -- Separator
           CONCAT_WS(' ', tadd.street_number, tadd.street_name),
           tadd.apt_number,  tadd.city, 
           tadd.postal_code, tadd.country
         ) AS Address

From the docs:

CONCAT_WS(separator,str1,str2,...)

CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.

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

ypercubeᵀᴹ
  • 97,895
  • 13
  • 214
  • 305
10

Convert NULL values in empty string by wrapping it in COALESCE or IFNULL:

IFNULL:

SELECT
    CONCAT(IFNULL(tadd.street_number,''),
        ' ',IFNULL(tadd.street_name,''),
        ', ',IFNULL(tadd.apt_number,''),
        ', ',IFNULL(tadd.city,''),
        ', ',IFNULL(tadd.postal_code,''),
        ', ',IFNULL(tadd.country,'')) AS 'Address'
FROM db.tbl;

COALESCE:

SELECT
    CONCAT(COALESCE(tadd.street_number,''), 
        ' ',COALESCE(tadd.street_name,''),
        ', ',COALESCE(tadd.apt_number,''),
        ', ',COALESCE(tadd.city,''),
        ', ',COALESCE(tadd.postal_code,''),
        ', ',COALESCE(tadd.country,'')) AS 'Address'
FROM db.tbl
oNare
  • 3,181
  • 2
  • 20
  • 35
3
CONCAT(
    tadd.street_number, ' ', tadd.street_name, ', ',
-- concat() will return null if one is null, so ifnull returns empty string in that case
    IFNULL(CONCAT(tadd.apt_number, ', '), ''),
    tadd.city, ', ', tadd.postal_code, ', ',tadd.country
) AS 'Address'
jkavalik
  • 5,080
  • 1
  • 12
  • 20
1
CONCAT_WS('',         -- hack, empty delimiter
        tadd.street_number, ' ',
        tadd.street_name,', ',
        CONCAT(tadd.apt_number,', '), -- hack, this line will become NULL, when apt_number is null, and will be omitted with delimiter
        tadd.city,', ',
        tadd.postal_code,', ',
        tadd.country) AS 'Address'

I don't know mysql, but in MSSQL (TQSL) the solution looks like:

SELECT
        tadd.street_number + ' ' +
        tadd.street_name + ', ' +
        ISNULL(tadd.apt_number  + ', ', '') +
        tadd.city + ', ' +
        tadd.postal_code + ', ' +
        tadd.country AS 'Address'

Moreover, you can omit all NULL fields, not just apt_number (mysql again):

SELECT CONCAT_WS(', ',
        CONCAT(tadd.street_number, ' ', tadd.street_name),
        tadd.apt_number,
        tadd.city,
        tadd.postal_code,
        tadd.country) AS 'Address'
maxkoryukov
  • 223
  • 2
  • 8