Using SELECT to easily evaluate BOOLEAN

I want to check some grades without working in any table. For example, you can write

SELECT 1+1 >2 

I want to achieve something like this:

 SELECT 2 > 1 >FALSE 

I know that most engines do not have a logical data type concept, but I don’t know how their internal work (even if I think that all <> 0 is true , as in C). Anyway, the response format really doesn't matter, be it true/false or 0/1

+6
source share
5 answers
 SELECT CASE WHEN 2 > 1 THEN 'True' ELSE 'False' END 
+12
source
 SELECT CASE WHEN 2 > 1 THEN 1 ELSE 0 END 
+4
source
 SELECT CASE WHEN 2 > 1 THEN cast(0 as bit) ELSE cast(1 as bit) END 
+4
source

Edit:

SQL Server 2012 (Denali):

 SELECT IIF(2 > 1, 'TRUE', 'FALSE') 
+3
source

In Mysql and Oracle, and apparently SQL-Server, you can use nullif (ignore from dual for SQL-Server), so assuming you accept null as true, you can handle equality testing this way without a messy case or deal with strings.

 SQL> select nullif(1,0) from dual; NULLIF(1,0) ----------- 1 SQL> select nullif(1,1) from dual; NULLIF(1,1) ----------- SQL> 
+1
source

Source: https://habr.com/ru/post/902935/


All Articles