14

There is a field show which is a boolean type in postgesql.

I want to write a sql to update that table, to toggle the value of show. If it's true, it becomes false, if it's false, it becomes true.

Is it possible?

23

This will do:

SET show = NOT show

so a value of

TRUE becomes FALSE,

FALSE becomes TRUE,

UNKNOWN (NULL) stays UNKNOWN.

  • 1
    @a_horse_with_no_name: Yeah, but I had to write something to cover the 30 characters limitation. Since we already had TRUE and FALSE, a variation on UNKNOWN was appropriate :) – ypercubeᵀᴹ Mar 29 '12 at 6:38
  • This is awesome, elegant, and exactly what I wanted. Ultra glad this isn't only possible MySQL. – sudowned Sep 3 '12 at 4:15
1

The following will turn FALSE or NULL to TRUE, and TRUE to FALSE:

UPDATE tablename SET fieldname = NOT COALESCE( fieldname, 'f' ) WHERE keyvalue = ?;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.