¿Cómo excluir valores NULL dentro de CONCAT MySQL?


14

Si tengo esto, tadd es la Addresstabla:

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

¿Hay alguna manera de excluir el número_aptivo si no existe?

Estaba pensando en:

WHERE tadd.apt_number IS NOT NULL

Pero solo devolverá esas filas apt_number, e incluso si algo funciona, ¿cómo puedo lidiar con esa coma adicional?

Si es un duplicado, publique un enlace en los comentarios.

Respuestas:


18

Si desea omitir NULLvalores (pero no cadenas vacías), puede usar la CONCAT_WS()función:

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

De los documentos:

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

CONCAT_WS()significa Concatenar con separador y es una forma especial de CONCAT(). El primer argumento es el separador para el resto de los argumentos. El separador se agrega entre las cadenas a concatenar. El separador puede ser una cadena, al igual que el resto de los argumentos. Si el separador es NULL, el resultado es NULL.

CONCAT_WS()no omite cadenas vacías. Sin embargo, omite cualquier NULLvalor después del argumento separador.


8

Convierta NULLvalores en una cadena vacía envolviéndola COALESCEo 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;

JUNTARSE:

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'

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'

No sé mi sql, pero en MS SQL (TQSL) la solución se ve así:

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

Además, puede omitir todos los NULLcampos, no solo apt_number (mysql nuevamente):

SELECT CONCAT_WS(', ',
        CONCAT(tadd.street_number, ' ', tadd.street_name),
        tadd.apt_number,
        tadd.city,
        tadd.postal_code,
        tadd.country) AS 'Address'
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.