Click here to Skip to main content
15,921,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo,

I want to do a select statement which returns a boolean value

e.g.
SQL
select firstname, lastname, (income > 30000) as richperson from tbl_person


how can I syntactically do this with mssql? If i do it like shown above I get a syntax error
Posted
Updated 3-Jan-12 5:18am
v2

you can do with case statement.

try this query..

SQL
select firstname, lastname, (case when income > 30000 then 'true' else 'false' end) as richperson from tbl_person


this will return true or false as varchar. if you want in boolean replace with 1 and 0 respectively.

SQL
select firstname, lastname, (case when income > 30000 then 1 else 0 end) as richperson from tbl_person

hope it helps..
 
Share this answer
 
v3
Try:
SQL
SELECT firstname, lastname, CASE WHEN income > 30000 THEN 1 ELSE 0 END AS richperson FROM tbl_person
 
Share this answer
 
First of all, SQL Server does not have a boolean data type. The closest equivalent is a bit. So this means that depending on your needs, you may have to 'translate' the return value in your code before you use it.

The SQL statements provided by Karthik Harve and OriginalGriff are perfectly valid, but to throw another variant, you could also do something like:
SQL
SELECT p.firstname,
       p.lastname,
       CAST(p.income/30001 AS bit) AS richperson
FROM tbl_person p
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900