postgresqlbooleansql-null

Postgres: Update Boolean column with false if column contains null


I have existing table called test with two columns id and utilized

Here utilized contains three values in my table null, false and true. Here null is default for utilized

I want to update the rows with utilized false where utilized is null so I tried below query in Postgres but it didn’t work for me.

  update test set utilized=false where utilized=null;

Solution

  • You must use IS NULL to check for the presence of a NULL value:

    UPDATE test
    SET utilized = false
    WHERE utilized IS NULL;
    

    In SQL, unlike other places such as Java or C#, NULL is a special value meaning something like "not known." Therefore, comparing a column to NULL using the = operator is also unknown, because it might or might not be true. Instead, use IS NULL or IS NOT NULL to directly compare a column to NULL.