Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if else condition in where clause in ms sql server

I'm trying to retrieve the records from the joined tables based on 2 condition like below:

Where if(b.enddate is null)
         a.starttime >= b.starttime
      else
         a.starttime >= b.starttime and a.endtime <= b.endtime

I have seen examples using case when but the result is not what I wanted. Please assist me to convert this condition into a proper sql format.

like image 964
Gary Avatar asked Dec 07 '22 14:12

Gary


1 Answers

You don't need to use case expression, this can be done using OR in WHERE clause

... Where 
( b.enddate is null and a.starttime >= b.starttime)
or     
( b.enddate is not null and a.starttime >= b.starttime and a.endtime <= b.endtime)
like image 162
DhruvJoshi Avatar answered Dec 14 '22 23:12

DhruvJoshi