Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Else Statement within an inner join

Tags:

sql

I have a table jobs with a column StartDate that is often null. If it is not null this is what I want to inner join with a table quarters on, otherwise I want to inner join on some other conditions. This is sort of what I want:

INNER JOIN Quarters q
ON  (IF j.StartDate IS NOT NULL (j. StartDate BETWEEN GETDATE() and q.EndDate)
     ELSE **Some other condition**)

The error that comes up when this is run is that there is incorrect syntax near the keyword 'IF'

Does anyone know the correct syntax for this?

Thanks in advance for your help!

like image 970
Cat Avatar asked Dec 22 '22 12:12

Cat


2 Answers

INNER JOIN Quarters q ON
    (j.StartDate IS NOT NULL AND j. StartDate BETWEEN GETDATE() and q.EndDate)
OR
    (j.StartDate IS NULL AND **Some other condition**)
like image 64
Dark Falcon Avatar answered Jan 02 '23 08:01

Dark Falcon


The easiest way to handle this is just to treat it as a logical operation:

ON    (j.StartDate IS NOT NULL 
       and j. StartDate BETWEEN GETDATE() and q.EndDate)
   OR (some other condition)
like image 42
Allan Avatar answered Jan 02 '23 06:01

Allan