I am writing a pgplsql function in which I have a variable var that could be NULL or valorized. In a later query I do:
SELECT * FROM table WHERE column = var
that, in case of a NULL var, becomes
SELECT * FROM table WHERE column = NULL
So the query fails with NULL, because, as PostgreSQL documentation says,
The null value represents an unknown value, and it is not known whether two unknown values are equal
I resolved it with a CASE statement:
SELECT * FROM table WHERE
( CASE WHEN var IS NULL THEN column IS NULL ELSE column = var END ) = TRUE
But I am unsure wether this is the best way to resolve the question… do you have any good alternative?
1 Answer