Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 775
Hemadri Dasari Avatar asked Apr 18 '26 03:04

Hemadri Dasari


1 Answers

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.

like image 56
Tim Biegeleisen Avatar answered Apr 20 '26 16:04

Tim Biegeleisen



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!