By default, SQL Server doesn’t allow an operation like this:
[source:sql]SELECT SUM(blnBitColumn) FROM tblTable;
[/source]
In order to achieve this result, you must first convert the bit column to a numeric type:
[source:sql]SELECT SUM(CONVERT(int,blnBitColumn)) FROM tblTable;
[/source]
This counts the number of times the bit is true.
If you want to get the flip-side of that to see how many times the bit is false, just subtract the total number of bits from the positive:
[source:sql]SELECT COUNT(blnBitColumn)-SUM(CONVERT(int,blnBitColumn)) FROM tblTable;
[/source]
Tags: SQL
A few ways to tackle this issue.
Counting true bits
– SELECT COUNT(NULLIF(blnBitColumn,0)) From tblTable
Counting False bits
– SELECT COUNT(NULLIF(blnBitColumn,1)) From tblTable