agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
Question about WHERE CASE
3+ messages / 3 participants
[nested] [flat]

* Question about WHERE CASE
@ 2019-09-05 11:04 Mike Martin <redtux1@gmail.com>
  2019-09-05 11:42 ` Re: Question about WHERE CASE Martin Stöcker <martin.stoecker@stb-datenservice.de>
  2019-09-05 13:46 ` Re: Question about WHERE CASE Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 3+ messages in thread

From: Mike Martin @ 2019-09-05 11:04 UTC (permalink / raw)
  To: pgsql-sql@lists.postgresql.org

I have come across the following construct which works in postgres

WHERE CASE WHEN $1=dir THEN TRUE ELSE metadir=$1 END

via (
https://stackoverflow.com/questions/49859153/filter-with-yes-no-and-all-in-postgres
)

case when ('yes'= lower($1)) then (phone_number is not null)
     when ('no'=lower($1)) then (phone_number is null)
     else true end

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

thanks

Mike

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Question about WHERE CASE
  2019-09-05 11:04 Question about WHERE CASE Mike Martin <redtux1@gmail.com>
@ 2019-09-05 11:42 ` Martin Stöcker <martin.stoecker@stb-datenservice.de>
  1 sibling, 0 replies; 3+ messages in thread

From: Martin Stöcker @ 2019-09-05 11:42 UTC (permalink / raw)
  To: pgsql-sql@lists.postgresql.org


Hi,

where requires a boolean expression, for instance
select * from stuff where true;

Hence "CASE WHEN $1=dir THEN TRUE ELSE metadir=$1 END" returns a boolean 
value everything is fine.

Regards martin



Am 05.09.2019 um 13:04 schrieb Mike Martin:
> I have come across the following construct which works in postgres
>
> WHERE CASE WHEN $1=dir THEN TRUE ELSE metadir=$1 END
>
> via 
> (https://stackoverflow.com/questions/49859153/filter-with-yes-no-and-all-in-postgres)
> |casewhen('yes'=lower($1))then(phone_number 
> isnotnull)when('no'=lower($1))then(phone_number isnull)elsetrue end |
> |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 |
> |thanks |
> |Mike |

-- 

Widdersdorfer Str. 415, 50933 Köln; Tel. +49 / 221 / 9544 010
HRB Köln HRB 75439, Geschäftsführer: Dr. Dirk Goldner, ppa. Melanie Lillich

Attachments:

  [image/jpeg] icapbifdkakidkdn.jpg (13.6K, ../../da8bed3e-b178-a788-075e-93c3cbfb8b88@stb-datenservice.de/3-icapbifdkakidkdn.jpg)
  download | view image

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Question about WHERE CASE
  2019-09-05 11:04 Question about WHERE CASE Mike Martin <redtux1@gmail.com>
@ 2019-09-05 13:46 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2019-09-05 13:46 UTC (permalink / raw)
  To: Mike Martin <redtux1@gmail.com>; +Cc: pgsql-sql@lists.postgresql.org

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





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2019-09-05 13:46 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-09-05 11:04 Question about WHERE CASE Mike Martin <redtux1@gmail.com>
2019-09-05 11:42 ` Martin Stöcker <martin.stoecker@stb-datenservice.de>
2019-09-05 13:46 ` Tom Lane <tgl@sss.pgh.pa.us>

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