Sergio Tapia
Sergio Tapia

Reputation: 41138

How can I declare a Boolean parameter in SQL statement?

How can I declare a Boolean parameter in SQL statement?

Upvotes: 54

Views: 200986

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

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.

Upvotes: 75

Eric
Eric

Reputation: 95133

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.

Upvotes: 34

Related Questions