Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL -- create column in SELECT to test equality

Tags:

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.

like image 381
Matt H. Avatar asked May 17 '11 02:05

Matt H.


People also ask

How do I check if a column is equal in SQL?

In SQL, problems require us to compare two columns for equality to achieve certain desired results. This can be achieved through the use of the =(equal to) operator between 2 columns names to be compared.

How do I check if two values are equal in SQL?

SQL Equal to ( = ) operator The equal to operator is used for equality test within two numbers or expressions.

Do we use == in SQL?

As a result, SQL doesn't have the problem of ambiguity of = meaning either assignment or equality check. As a result, there is no problem with using = to check equality. On the other hand, in a programming language such as Java, single = is used for assignments, while == is used for comparison.

How do you check for equal conditions in SQL Server?

In SQL Server database, Equality Operator "=" is used to test for equality in a query.


1 Answers

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 
like image 89
Buck Hicks Avatar answered Nov 09 '22 22:11

Buck Hicks