Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR shorthand in Oracle SQL?

Tags:

sql

oracle

I have a query that I need to check around 20 different columns for a 0 value.

Rather than doing:

WHERE BOOK <> 0 OR ALLO <> 0 OR ...

Is there a quicker way of doing it? Something like: WHERE (BOOK,ALLO,...) <> 0

like image 773
Lock Avatar asked Feb 25 '13 00:02

Lock


1 Answers

Although it doesn't run anywhere else (MySQL, SQL-Server, Postgres) and it's probably not SQL-standard, it works in Oracle:

WHERE 0 <> ANY (BOOK, ALLO, ...)

Tested in SQL-Fiddle


There is also another way that is standard and works in MySQL and Postgres, but not in Oracle:

WHERE (0, 0, ...) <> (BOOK, ALLO, ...) 

And another standard way (using a Table Values Constructor) that works in Postgres and SQL-Server 2012:

WHERE 0 <> ANY (VALUES (BOOK), (ALLO), ...)
like image 69
ypercubeᵀᴹ Avatar answered Oct 15 '22 09:10

ypercubeᵀᴹ