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.

4 Answers 4

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.

0
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
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'
0
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'

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.