Inweo
Inweo

Reputation: 183

Postgresql update column when passed condition

Is it possible, and if so, how can the following change be achieved?

Given the table:

param_tab
param_id serial
value integer
anothervalue integer
update_date TIMESTAMP

I would like to do something similar to this:

UPDATE param_tab pt
CASE WHEN CONDITION THEN pt.value = 14, pt.anothervalue = 20 END
pt.update_date = someTimestamp;

So update_date is always updated and value and anothervalue only in case of some condition

Upvotes: 2

Views: 5803

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246788

Use the CASE statement in the correct place:

UPDATE param_tab pt
SET value = CASE WHEN condition THEN 14 ELSE pt.value END,
    anothervalue = CASE WHEN condition THEN 20 ELSE pt.anothervalue END,
    update_date = someTimestamp;

Upvotes: 7

Related Questions