Let’s say I have a table that has an int primary key, a varchar and a bit value. Please keep in mind that I’m not actually using anything like this but it illustrates what I’m trying to do in a SELECT statement.
Faux Table:
CREATE TABLE HorribleSample(
Id INT IDENTITY(1,1) NOT NULL,
Code VARCHAR(6) NOT NULL,
IsTrue BIT NOT NULL,
CONSTRAINT PK_HorribleSample PRIMARY KEY CLUSTERED(Id ASC)
)
Sample Data:
INSERT INTO HorribleSample VALUES('HELLO', 0)
INSERT INTO HorribleSample VALUES('WORLD', 1)
INSERT INTO HorribleSample VALUES('HELLO', 1)
INSERT INTO HorribleSample VALUES('WORLD', 1)
What I’m trying to do is select the lowest bit value from the result set. I thought I could do:
SELECT MIN(IsTrue)
FROM HorribleSample
WHERE Code = 'HELLO'
But I get an error stating that I can’t use MIN on a bit field.
When the code is HELLO, I would expect 0, but when it’s WORLD I would expect 1. How can I accomplish this? Is it even possible?
Try this: