What is a canonical expression for the following:
NULL -> false
false -> false
true -> true
What I am using is:
select (foo is not null and foo)
which seems to work fine, but I'm wondering if there is a standard convention for this.
What is a canonical expression for the following:
NULL -> false
false -> false
true -> true
What I am using is:
select (foo is not null and foo)
which seems to work fine, but I'm wondering if there is a standard convention for this.
I just looked it up. COALESCE()
is ansi SQL. So SELECT (foo, false)
would give foo or false.
If with canonical you means a standardized way, not DBMS/vendor-depending, then use CASE WHEN.
It is ANSI
, and easy:
SELECT CASE WHEN (foo is null)
THEN false
ELSE foo
END
AS YourStuff
...
Or Coalesce
as stated in the other answers.
Their performance are identical: Coalesce vs Case
You can use COALESCE
or ISNULL
For MySQL you can also use IFNULL
SELECT COALESCE(foo,false)
or
SELECT ISNULL(foo,false)
or
SELECT IFNULL(foo,false)