Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Firebird e Interbase (https://www.clubdelphi.com/foros/forumdisplay.php?f=19)
-   -   ¿Error en COALESCE? (https://www.clubdelphi.com/foros/showthread.php?t=92621)

Angel.Matilla 12-12-2017 13:20:07

¿Error en COALESCE?
 
Estoy probando este query con SQL Manager:
Código:

SELECT Apellidos, Referencia, Nif, Codigo, COALESCE(Referencia, Nif, CAST(Codigo AS VARCHAR(10)))
FROM Persona
ORDER BY 2,3,4,5

Según lo que llevo leido, la cláusula COALESCE me devolvería un valor NULL si los tres argumentos lo son. Sin embargo me está devolviendo esto:
Código PHP:

Apellidos      Referencia Nif       Codigo  COALESCE
ARIAS ORGANERO            06208661H 842
RIVERA GOMEZ              06238975H 12476
VAQUERO LIZANO 1000       06172129X 15257   1000 

Y yo entiendo que en las dos primeras líneas, al estar vacío el campo referencia, debería devolver el valor de Nif. ¿Qué estoy haciendo mal?

Casimiro Notevi 12-12-2017 13:33:33

¿Has comprobado que son null, no cadena vacía ''?



Angel.Matilla 12-12-2017 13:40:50

Cita:

Empezado por Casimiro Notevi (Mensaje 523327)
¿Has comprobado que son null, no cadena vacía ''?

Lo cierto es que no; he dado por supuesto que se iban a comportar igual. Y es cierto, eran candenas vacías :o

Casimiro Notevi 12-12-2017 13:45:07

Cita:

Substituting NULL with a value
The COALESCE function

The COALESCE function in Firebird 1.5 and higher can convert NULL to most anything else. This enables you to perform an on-the-fly conversion and use the result in your further processing, without the need for “if (MyExpression is null) then” or similar constructions. The function signature is:

COALESCE( Expr1, Expr2, Expr3, ... )

COALESCE returns the value of the first non-NULL expression in the argument list. If all the expressions are NULL, it returns NULL.
https://www.firebirdsql.org/manual/n...nversions.html

jhonny 12-12-2017 16:02:56

Debido a lo de la cadena vacía, puedes combinar con NULLIF, así:

Código SQL [-]
SELECT Apellidos, Referencia, Nif, Codigo, COALESCE(NullIf(Referencia, ''), NullIf(Nif, ''), NullIf(CAST(Codigo AS VARCHAR(10), '') ))
FROM Persona
ORDER BY 2,3,4,5

Angel.Matilla 12-12-2017 17:44:54

Cita:

Empezado por jhonny (Mensaje 523334)
Debido a lo de la cadena vacía, puedes combinar con NULLIF

Gracias, no se me había ocurrido.


La franja horaria es GMT +2. Ahora son las 17:11:34.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi