56

How can I declare a Boolean parameter in SQL statement?

2 Answers 2

76

The same way you declare any other variable, just use the bit type:

DECLARE @MyVar bit
Set @MyVar = 1  /* True */
Set @MyVar = 0  /* False */

SELECT * FROM [MyTable] WHERE MyBitColumn = @MyVar

Note this is semantically different from a true boolean. The 1/0 values won't always just map to true/false the way you might expect, especially when you add NULL to the mix.

Sign up to request clarification or add additional context in comments.

1 Comment

Although bit is the closest match, bit isn't boolean. You can't do this in TSQL, for instance: IF @isDeleted .... You must do this instead IF @ isDeleted = 1 ....
34

SQL Server recognizes 'TRUE' and 'FALSE' as bit values. So, use a bit data type!

declare @var bit
set @var = 'true'
print @var

That returns 1.

1 Comment

I think in comparison, one needs to do (@var = 1) NOT JUST (@var), at least in the version I use, especially for SSRS reports (if TSQL is used there) -- very similar comment to that by Darren Griffith above...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.