SQL Server Q&A: Does SQL Server have a boolean data type? Is that a bit?

SQL Server Q&A: Does SQL Server have a boolean data type? Is that a bit?

Simple answer: No

SQL Server has a concept of boolean data type but it doesn’t have an actual boolean data type. I can’t declare a column with a boolean data type, and I can’t use it that way. However, the concept of boolean data is present.

For example, if I write the following:

SELECT * FROM Sometable WHERE Somecolumn = Someothercolumn

the outcome of that comparison is TRUE, FALSE, or (don’t forget) NULL. And remember that NULL isn’t a value; it’s the lack of a value. WHERE clauses are satisfied when the value of the predicate is TRUE.

So, there is a boolean concept in the language.

However, note that I cannot write this:

SELECT Somecolumn = Someothercolumn AS TheSameValue FROM Sometable

I can’t just return a TRUE or FALSE value.

If I write it without the alias, it will work:

SELECT Somecolumn = Someothercolumn AS TheSameValue FROM Sometable

but in that case, I’m returning the value of Someothercolumn aliased (named as) Somecolumn. I’m not returning the comparison.

But isn’t that just the same as a bit?

No. I can’t use a bit value like a boolean. For example, I can’t just write this:

IF ISNUMERIC(‘somestring’) BEGIN

END

Instead, I have to write this:

IF ISNUMERIC(‘somestring’) <> 0 BEGIN

END

Note that I could have written “= 1” but I prefer with bit values to always compare them to zero across all languages, given some languages use -1 for TRUE and others use 1 for TRUE.

2018-10-01