I know that SQL Server does not have boolean data type and your best option is to use BIT data type.
My question is to what boolean expressions evaluate. For example if I have the statement
expr1 AND expr2
and if expr1 is true and expr2 is false, do they internally evaluate to BITs with values 1 and 0? And then the AND operator checks for BITs?
I think this is not the case because then the following should work:
select 1 where 1 and 0
So does sql server internally have a boolean data type?
The following work
select 1 where 1 = 0
select 1 where 1 = 0 and 0 = 0
but this
select 1 where 1 and 0
reports
Msg 4145, Level 15, State 1, Line 1
An expression of non-boolean type specified in a context where a condition is expected, near ‘and’.
which means that internally sql server handles the expressions as booleans, but why don’t have access to that data type.
SQL Server does have a
Booleandata type. You can open the Logical Operators (Transact-SQL) manual page and find the following statement:It’s just that you can’t use this type in the same way you can use other Transact-SQL data types. For instance, you can’t declare boolean variables or arguments, add boolean columns to tables, cast to/from a boolean. But you can have boolean expressions and use them in contexts where they are required (
WHERE,ON, check constraints…). You can also apply boolean operators to those expressions:AND,NOTet al. (Operators like<,=,LIKEand other can also be considered boolean, in the sense that they return boolean results, but their operands are actually never booleans.)So, to summarise, there is a boolean type in SQL Server but its use is limited, as described above. Why? My answer may be a silly one, sorry, but that’s one that I’m satisfied with: this is the way they chose it to be.