Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the result of a comparison in SQL statement

Tags:

sql

How would I achieve the following:

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

so that it would yield the following results:

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

I'm using SQL Server but a cross DBMS example would be good.

like image 311
Steve Whitfield Avatar asked Mar 17 '11 11:03

Steve Whitfield


1 Answers

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

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

In the place of condition, you can use any variable or any column values too. Basically an expression.

like image 144
Sachin Shanbhag Avatar answered Sep 28 '22 01:09

Sachin Shanbhag