206

How to return a boolean value on SQL Select Statement?

I tried this code:

SELECT CAST(1 AS BIT) AS Expr1
FROM [User]
WHERE (UserID = 20070022)

And it only returns TRUE if the UserID exists on the table. I want it to return FALSE if the UserID doesn't exist on the table.

jkdev
  • 11,360
  • 15
  • 54
  • 77
mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95
  • 6
    Which dbms? Details of sql differ. – joshp Apr 30 '12 at 02:19
  • SQL Server does not support a Boolean type e.g. `SELECT WHEN CAST(1 AS BIT) THEN 'YES' END AS result` -- results in an error i.e. `CAST(1 AS BIT)` is not the same logical TRUE. – onedaywhen Apr 30 '12 at 07:38

9 Answers9

329

What you have there will return no row at all if the user doesn't exist. Here's what you need:

SELECT CASE WHEN EXISTS (
    SELECT *
    FROM [User]
    WHERE UserID = 20070022
)
THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT) END
Chad
  • 7,279
  • 2
  • 24
  • 34
  • 3
    why use asterisk, it is better if you use `1` instead of `*`. –  Mar 13 '15 at 06:04
  • 9
    @robertpeter07 - The two are equivalent, but `*` is more idiomatic. See [this question](http://stackoverflow.com/q/1597442/1121833). – Chad Mar 13 '15 at 15:12
  • If using on a WHILE loop would I have to enclose it within braces {} right after the 'WHILE' ? – full_prog_full Mar 10 '16 at 16:06
  • Can you add a column name to the returned value? – xMetalDetectorx Oct 23 '17 at 22:18
  • 3
    @xMetalDetectorx This worked for me to add the column name (the `AS bool` part is very important): `CAST( CASE WHEN EXISTS ( SELECT * FROM mytable WHERE mytable.id = 1) THEN TRUE ELSE FALSE END AS bool) AS nameofmycolumn` – Lucio Mollinedo Nov 29 '18 at 23:48
  • Really an extremely minor point... but I would put the cast around the case rather than having to cast two expressions. – BVernon Feb 09 '21 at 06:34
41

Possibly something along these lines:

SELECT CAST(CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS BIT)
FROM dummy WHERE id = 1;

http://sqlfiddle.com/#!3/5e555/1

Salman A
  • 262,204
  • 82
  • 430
  • 521
cableload
  • 4,215
  • 5
  • 36
  • 62
27

Given that commonly 1 = true and 0 = false, all you need to do is count the number of rows, and cast to a boolean.

Hence, your posted code only needs a COUNT() function added:

SELECT CAST(COUNT(1) AS BIT) AS Expr1
FROM [User]
WHERE (UserID = 20070022)
Stewart
  • 17,616
  • 8
  • 52
  • 80
  • 9
    Doing the `Exists(` test is much faster than doing a `Count(1)` test on tables with large numbers of rows. – Scott Chamberlain Apr 03 '14 at 02:06
  • 5
    Probably. I made no claims to performance in my answer, just the minimal code change to achieve what the OP wanted. However, if column `UserID` is indexed (or is even the PK) surely you're going straight to the one unique row that exists (or not). – Stewart Apr 03 '14 at 10:23
19

Use 'Exists' which returns either 0 or 1.

The query will be like:

SELECT EXISTS(SELECT * FROM USER WHERE UserID = 20070022)
Ananthi
  • 413
  • 7
  • 13
11
select CAST(COUNT(*) AS BIT) FROM [User] WHERE (UserID = 20070022)

If count(*) = 0 returns false. If count(*) > 0 returns true.

ngrashia
  • 9,869
  • 5
  • 43
  • 58
G.Noulas
  • 442
  • 6
  • 10
7

I do it like this:

SELECT 1 FROM [dbo].[User] WHERE UserID = 20070022

Seeing as a boolean can never be null (at least in .NET), it should default to false or you can set it to that yourself if it's defaulting true. However 1 = true, so null = false, and no extra syntax.

Note: I use Dapper as my micro orm, I'd imagine ADO should work the same.

RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
4

For those of you who are interested in getting the value adding a custom column name, this worked for me:

CAST(
    CASE WHEN EXISTS ( 
           SELECT * 
           FROM mytable 
           WHERE mytable.id = 1
    ) 
    THEN TRUE 
    ELSE FALSE 
    END AS bool) 
AS "nameOfMyColumn"

You can skip the double quotes from the column name in case you're not interested in keeping the case sensitivity of the name (in some clients).

I slightly tweaked @Chad's answer for this.

Lucio Mollinedo
  • 2,295
  • 1
  • 33
  • 28
  • Msg 102, Level 15, State 1, Line 8 Incorrect syntax near 'CAST'. Msg 156, Level 15, State 1, Line 12 Incorrect syntax near the keyword 'THEN'. – ShaneC Jun 26 '20 at 10:41
  • @ShaneC I tested this code on PostgreSQL 9.X and it worked fine. What server are you using? – Lucio Mollinedo Jun 29 '20 at 16:12
1

Notice another equivalent problem: Creating an SQL query that returns (1) if the condition is satisfied and an empty result otherwise. Notice that a solution to this problem is more general and can easily be used with the above answers to achieve the question that you asked. Since this problem is more general, I am proving its solution in addition to the beautiful solutions presented above to your problem.

SELECT DISTINCT 1 AS Expr1
FROM [User]
WHERE (UserID = 20070022)
Dean Leitersdorf
  • 1,303
  • 1
  • 11
  • 22
1
DECLARE @isAvailable      BIT = 0;

IF EXISTS(SELECT 1  FROM [User] WHERE (UserID = 20070022))
BEGIN
 SET @isAvailable = 1
END

initially isAvailable boolean value is set to 0