John
John

Reputation: 1852

flipping boolean value in postgresql

I have a Boolean type field in my table (PostgreSQL 9.0)

Is there a way to write a query that update this field to the opposite value without knowing what is currently there?

if it's True then it will be updated to False.

if it's False then it will be updated to True.

Basically I'm asking if there is a mechanism that allows treating Boolean type same as 1 bit. For example in C if x can be {1,0} you can simply write x=!x and if it was 0 it will be 1, if it was 1 it will be 0. No need for IF statment etc...

Upvotes: 4

Views: 2186

Answers (1)

Kirill Slatin
Kirill Slatin

Reputation: 6143

Why not to use operator NOT?

Update tableName
set c = NOT c 
where ...

http://www.postgresql.org/docs/current/static/functions-logical.html

Example in SqlFiddle

Upvotes: 8

Related Questions