How can I use SUM for bit columns?
SELECT SUM(CAST(bitColumn AS INT))
FROM dbo.MyTable
need to cast into number
or another solution -
SELECT COUNT(*)
FROM dbo.MyTable
WHERE bitColumn = 1
You can achieve by using CONVERT
,
SELECT SUM(CONVERT(INT, bitColumn)) FROM MyTable
You could consider 0 as nulls and simply count the remaining values:
SELECT count(nullif(bitColumn, 0))
FROM MyTable;
SELECT SUM(bitColumn * 1) FROM dbo.MyTable
Converts the bit into int, by multiplication, clean and simple