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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s