Return Select As Boolean

In this quick little post, I would like to document how to travers a SQL SELECT statement to return back a BIT/BOOLEAN. Suppose, you want to return true of false, based on a condition and your application can simply read this BIT/BOOLEAN and show appropriate view/response to end user.


SELECT [Account].Balance,

CAST(CASE

WHEN [Account].Balance - @Value > 0

THEN 1 ELSE 0

END AS Bit)

AS MoneyAvailable

FROM [dbo].[UserAccounts] [Account]

WHERE [Account].[UserId] = 13

The above statement will return a nice little one column ‘MoneyAvailable’ with a 1 or a 0 to denote if that account has available balance or not. Good stuff.

Advertisements