agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Mike Martin <redtux1@gmail.com>
Cc: pgsql-sql@lists.postgresql.org
Subject: Re: Question about WHERE CASE
Date: Thu, 05 Sep 2019 09:46:30 -0400
Message-ID: <5160.1567691190@sss.pgh.pa.us> (raw)
In-Reply-To: <CAOwYNKYVz67+qK6QfSiJVhCA8fAXzpHLS-L9HG=SEhOUzJ0A1w@mail.gmail.com>
References: <CAOwYNKYVz67+qK6QfSiJVhCA8fAXzpHLS-L9HG=SEhOUzJ0A1w@mail.gmail.com>

Mike Martin <redtux1@gmail.com> writes:
> I was always under the impression that a case expression could only be
> on the right side of a where expression ie:
> WHERE fieldname=<cse expression>
> Is this a postgres extention, cant find any documentation on this

SQL has always had two forms of CASE: you can do

CASE
  WHEN boolean_expression1 THEN value1
  [ WHEN boolean_expression2 THEN value2 ... ]
  [ ELSE valueN ]
END

or you can do

CASE test_value
  WHEN comparison_value1 THEN value1
  [ WHEN comparison_value2 THEN value2 ... ]
  [ ELSE valueN ]
END

The latter is effectively the same as

CASE
  WHEN test_value = comparison_value1 THEN value1
  [ WHEN test_value = comparison_value2 THEN value2 ... ]
  [ ELSE valueN ]
END

except test_value is only supposed to be evaluated once.

This goes back at least as far as SQL-92.

It is documented, see
https://www.postgresql.org/docs/current/functions-conditional.html

			regards, tom lane





view thread (3+ messages)

Message-ID: <5160.1567691190@sss.pgh.pa.us>
Permalink:  ../5160.1567691190@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/5160.1567691190@sss.pgh.pa.us

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, redtux1@gmail.com, pgsql-sql@lists.postgresql.org
  Subject: Re: Question about WHERE CASE
  In-Reply-To: <5160.1567691190@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox