0 votes
1 view
in SQL by (20.3k points)

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 only return TRUE value if the UserID exists on the table. I want it to return FALSE value if the UserID doesn't exist on the table. Thank you very much.

1 Answer

0 votes
by (40.4k points)

If the user doesn't exist, then they will return no row at all. 

Use the below code:

SELECT CASE WHEN EXISTS (

    SELECT *

    FROM [User]

    WHERE UserID = 20070022

)

THEN CAST(1 AS BIT)

ELSE CAST(0 AS BIT) END

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 17, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...