Does SQL have anything like C's conditional expression?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-sql(at)postgreSQL(dot)org
Subject: Does SQL have anything like C's conditional expression?
Date: 1998-07-20 20:01:33
Message-ID: 22075.900964893@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I have the following problem: given a specific record in a table
(identified by OID, in my case), always update field A to a new value,
and update field B to a new value only if field C contains 'OK'.

This is easy to do in two queries, say

UPDATE table SET fieldA = valueA WHERE oid = something;
UPDATE table SET fieldB = valueB WHERE oid = something AND fieldC = 'OK';

For speed reasons, I'm wondering if it can be done in one query.
It could be done with something like a C conditional expression:

UPDATE table SET
fieldA = valueA,
fieldB = (fieldC = 'OK') ? valueB : fieldB
WHERE oid = something;

but SQL doesn't seem to have that concept. Can anyone suggest
a fast way to do what I need to do?

regards, tom lane

Browse pgsql-sql by date

  From Date Subject
Next Message Peter T Mount 1998-07-22 05:42:49 Re: [SQL] Can I store bitmap graphics in a table?
Previous Message Dan Delaney 1998-07-20 17:55:24 Can I store bitmap graphics in a table?