Use COALESCE:
CopySELECT COALESCE(field_a, field_b)
COALESCE is an ANSI standard function that returns the first non-null value from the list of columns specified, processing the columns from left to right. So in the example, if field_a is null, field_b value will be displayed. However, this function will return NULL if there is no non-null value from the columns specified.
It's supported on MySQL (I've used it on 4.1), SQL Server (since v2000), Oracle 9i+...
Answer from OMG Ponies on Stack OverflowVideos
Use COALESCE:
CopySELECT COALESCE(field_a, field_b)
COALESCE is an ANSI standard function that returns the first non-null value from the list of columns specified, processing the columns from left to right. So in the example, if field_a is null, field_b value will be displayed. However, this function will return NULL if there is no non-null value from the columns specified.
It's supported on MySQL (I've used it on 4.1), SQL Server (since v2000), Oracle 9i+...
and another way to skin that cat (flexible for not just null comparisons)...
Copyselect if(field_a is not null, field_a, field_b) from...
I don't think there is any syntax that functions the same on both platforms.
Note Nz() is only available when using the Access user interface.
Here are a couple of suggestions that can be transformed to COALESCE fairly easily, though repeating the column is a pain:
Sample 1:
SELECT IIF([Amount] IS NULL, 0, [Amount]) FROM PaymentsDue;
Sample 2:
SELECT SWITCH([Amount] IS NULL, 0, TRUE, [Amount]) FROM PaymentsDue;
This will work, but it's clunky:
SELECT Amount
FROM PaymentsDue
WHERE Amount IS NOT NULL
UNION ALL
SELECT 0 AS Amount
FROM PaymentsDue
WHERE Amount IS NULL
Obviously if you have more than one column, this gets to be quickly unmanageable.