How to know if all the cells have the same value in some column

Shimmy Weitzhandler picture Shimmy Weitzhandler · Dec 23, 2009 · Viewed 18.1k times · Source

How to know if all the cells have the same value in some column (title changed)

I want to have a bit scalar value that tells me if all the values in a column equal something:

DECLARE @bit bit
SELECT @bit = TRUEFORALL(Name IS NOT NULL) FROM Contact

UPDATE

I now realized that I actually don't need the TrueForAll, what I do need is to make sure, that all values in a column are equal, for example, I want to know whether all Group.Items have the same price.

Answer

Sparky picture Sparky · Dec 23, 2009

Why not?

select count( distinct price) from table

If returns 1, all values are the same... Add

where price is not null

if need be