SQL nvl equivalente - senza istruzioni if ??/ case e amp; isnull & amp; fondersi
Domanda
Esistono funzioni equivalenti a nvl () in SQL?
O qualcosa di abbastanza vicino da poter essere usato allo stesso modo in determinati scenari?
AGGIORNARE:
no if dichiarazioni
no case case
no isnull
no coalesce
select nvl (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;
(expression)
SODIUFOSDIUFSDOIFUDSF
1 row(s) retrieved.
select isnull (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;
674: Routine (isnull) can not be resolved.
Error in line 1
Near character position 8
select coalesce (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;
674: Routine (coalesce) can not be resolved.
Error in line 1
Near character position 8
select decode(purge_date, NULL, "01/01/2009", purge_date) from id_rec where id=74115;
800: Corresponding types must be compatible in CASE expression.
Error in line 1
Near character position 57
Soluzione
Sembra che tu stia usando Informix.
AFAIK, c'è DECODE lì:
DECODE (field, NULL, 'is null, man', field)
dovrebbe fornire lo stesso risultato di NVL (field, 'is null, man')
Pubblica il nome esatto e la versione dell'RDBMS che stai utilizzando.
Altri suggerimenti
SQL Server: IsNull o COALESCE http://msdn.microsoft.com/en-us/library/ms184325. aspx
Sybase: funzione isnull http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.blocks/html/blocks/blocks162.htm
Postgres: Non riuscivo a trovarne uno anche se non ho controllato completamente. Suggerisce di selezionare dove è NULL e costruire da qui http://archives.postgresql.org/pgsql-sql/1998- 06 / msg00142.php
DB2 - COALESCE http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0000780.htm
Il problema con l'istruzione DECODE che sta generando l'errore 800 è semplice. '01 / 01/2009 '
è stato trattato come una stringa ed è in realtà il quarto argomento a generare l'errore.
Apprezzo il fatto che l'input e l'output di un'istruzione DECODE possano essere di diversi tipi di dati, quindi il motore richiede di essere più esplicito in questo caso. (Vuoi purge_date
cast come stringa o stringa '01 / 01/2009 '
, oppure l'argomento stringa analizzato come una data o la data originale? per il motore di sapere.
Prova questo:
SELECT DECODE(purge_date, NULL, '01/01/2009'::DATE, purge_date)
Puoi anche scrivere quel terzo argomento come:
DATE('01/01/2009')
MDY(1,1,2009)
a seconda della versione e delle preferenze personali.