13

Is there a difference in the following comparisons

#1

where x = true

#2

where x is true

#3

where x = '1'

#4

where x
0

2 Answers 2

13
  1. This is exactly the same as

    WHERE x
    

    It is TRUE, FALSE or NULL exactly when x is.

  2. This is the same as the first case, except when x is NULL, in which case it will be FALSE. So it is the same as

    WHERE coalesce(x, FALSE)
    
  3. This happens to be the same as the first case, since '1' is interpreted as TRUE. See the documentation:

    The datatype input function for type boolean accepts these string representations for the “true” state:

    true
    yes
    on
    1

My preferred way is the simplest:

WHERE x
Sign up to request clarification or add additional context in comments.

4 Comments

"...except when x is NULL, in which case it will be FALSE" -- Shouldn't that result in UNKNOWN rather than FALSE?
@TheImpaler: in a WHERE clause "unknown" is essentially the same as "false"
I'm not sure the WHERE clause in SELECT behaves the same way as in a CREATE INDEX... One filters when is true, while the other when is different from "false"... not sure anymore.
@TheImpaler x IS TRUE will return FALSE if x is NULL. My numbering corresponds to the numbering in the question.
6

Using is or is not will handle the case when the value is null. If you rely on =, the result of the comparison will also be null

select null is true as "is", null = true as "equal";
 is | equal
----+-------
 f  |
(1 row)

1 Comment

To expand on this a little: for non-null values, x is true and x = true will do the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.