Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL boolean comparison

Is there a difference in the following comparisons

#1

where x = true

#2

where x is true

#3

where x = '1'

#4

where x
like image 980
b00sted 'snail' Avatar asked Feb 23 '26 16:02

b00sted 'snail'


1 Answers

  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
like image 158
Laurenz Albe Avatar answered Feb 25 '26 05:02

Laurenz Albe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!