Matt H.
Matt H.

Reputation: 10806

SQL -- create column in SELECT to test equality

In SQL Server, I'm dealing with these columns:

tblSchedule


ID
StaffID
StartTime
EndTime

I want to include a boolean field in my result set that indicates whether StartTime and EndTime are equal. Something analogous to this:

SELECT StaffID, StartTime, EndTime, (StartTime = EndTime) AS AreEqual
FROM tblSchedule Where StaffID = xxx

But I'm not sure of the actual syntax for an operation like that.

Upvotes: 22

Views: 7397

Answers (2)

Buck Hicks
Buck Hicks

Reputation: 1574

I think this is what you are looking for

SELECT  StaffID
        , StartTime
        , EndTime
        , Case
          When StartTime = EndTime Then 1
          else  0
        End as AreEqual
FROM    tblSchedule
Where   StaffID = xxx

Upvotes: 27

p.campbell
p.campbell

Reputation: 100607

Try using a CASE WHEN in your SELECT statement; something like this:

SELECT CASE WHEN StartTime = EndTime THEN 1 ELSE 0 END AS AreEqual
FROM MyTable

Upvotes: 7

Related Questions