Ali7091 Ali7091 - 14 days ago 5
SQL Question

Get "And" operation on a field in SQL Server

i have table1 that have a "status" field of type bit

i wanna get true if all rows have status=1

i want something like this:

select And(status) from table1


is there any operation or function to do this in SQL Server?

Answer

If I get your question correctly, this can help you. You want true if all rows have status equal to 1.

IF EXISTS (SELECT 1 FROM Table1 WHERE status <> 1) THEN
   SELECT false
ELSE
   SELECT true
END IF