Return Boolean Value on SQL Select Statement Return Boolean Value on SQL Select Statement sql sql

Return Boolean Value on SQL Select Statement


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


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


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 Expr1FROM [User]WHERE (UserID = 20070022)