Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count bit field

Status
Not open for further replies.

Eli20

Programmer
Oct 30, 2003
119
MX
hi, im trying to make a query to get the total of values that are "true" in many bit fields.

for example, my table is like this:

id chk1 chk2 chk3
2 1 1 1
3 1 0 0
4 0 1 0
5 0 1 1

and i need the result to be:
2 3 2

grouped by date..

how can i count only the fields that are 1??

thank you very much

Eli
 
Someone else may know a better way to deal with bit fields, but this is how I've worked with them in the past:
Code:
select convert(char(10), SomeDate, 120) as SomeDate,
       sum(CAST(chk1 as tinyint)), 
       sum(CAST(chk2 as tinyint)), 
       sum(CAST(chk3 as tinyint))
from yourTable
group by convert(char(10), SomeDate, 120)

Good Luck!
 
Thank you very much, it worked perfect!!!


Eli
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top