La principale differenza tra i due è che IFNULL La funzione accetta due argomenti e restituisce il primo se non è NULL o il secondo se il primo è NULL .
COALESCE la funzione può accettare due o più parametri e restituisce il primo parametro non NULL, o NULL se tutti i parametri sono nulli, ad esempio:
SELECT IFNULL('some value', 'some other value');
-> returns 'some value'
SELECT IFNULL(NULL,'some other value');
-> returns 'some other value'
SELECT COALESCE(NULL, 'some other value');
-> returns 'some other value' - equivalent of the IFNULL function
SELECT COALESCE(NULL, 'some value', 'some other value');
-> returns 'some value'
SELECT COALESCE(NULL, NULL, NULL, NULL, 'first non-null value');
-> returns 'first non-null value'
AGGIORNAMENTO: MSSQL esegue un controllo di tipo e parametro più rigoroso. Inoltre, non ha IFNULL funzione ma invece ISNULL funzione, che deve conoscere i tipi degli argomenti. Pertanto:
SELECT ISNULL(NULL, NULL);
-> results in an error
SELECT ISNULL(NULL, CAST(NULL as VARCHAR));
-> returns NULL
Anche COALESCE la funzione in MSSQL richiede che almeno un parametro sia non null, quindi:
SELECT COALESCE(NULL, NULL, NULL, NULL, NULL);
-> results in an error
SELECT COALESCE(NULL, NULL, NULL, NULL, 'first non-null value');
-> returns 'first non-null value'