How can I declare a Boolean parameter in SQL statement?
Sql server does not expose a boolean data type which can be used in queries. Instead, it has a bit data type where the possible values are 0 or 1 . So to answer your question, you should use 1 to indicate a true value, 0 to indicate a false value, or null to indicate an unknown value.
Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.
There is boolean data type in SQL Server. Its values can be TRUE , FALSE or UNKNOWN . However, the boolean data type is only the result of a boolean expression containing some combination of comparison operators (e.g. = , <> , < , >= ) or logical operators (e.g. AND , OR , IN , EXISTS ).
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With