Select the comparison result in the SQL statement

How can I achieve the following:

select (1 < 2) as One, (1 > 2) as Two 

so that it gives the following results:

 One Two ----------------- True False 

I use SQL Server, but an example cross-database would be nice.

+8
sql
source share
3 answers

Assuming this is a SQL server, you can use CASE .

 select (case when (1 < 2) then 'True' else 'False' end) as one, (case when (1 > 2) then 'True' else 'False' end) as two from table 

Instead of a condition, you can use any variable or any column values. Basically an expression.

+13
source share

Well, in Oracle, you can do something like

 SELECT CASE WHEN 1 < 2 THEN 'TRUE' ELSE 'FALSE' END AS ONE, CASE WHEN 1 > 2 THEN 'TRUE' ELSE 'FALSE' END AS TWO FROM DUAL; 

Note that Oracle does not have a BOOLEAN type in the database (unlike PL / SQL, which has a BOOLEAN), so case expressions return character strings.

Share and enjoy.

0
source share

Use the case statement:

 declare @value1 int, @value2 int set @value1 = 1 set @value2 = 2 select case when (@value1 < @value2) then 'True' else 'False' end as One case when (@value1 > @value2) then 'False' else 'True' end as Two from table 

Depending on your needs, you can hard code the values, or you can do something like this when you can change the values. You can also combine the case statement into one column or split it to make comparisons with a smaller or equal type.

0
source share

All Articles