Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return "YES"/"NO" based on date comparison in sql [duplicate]

Tags:

sql

I want to write a sql query for the following excel-query. What should be the appropriate query?

IF

(
    (project. PA_SUBMIT_DATE )-(project. PA_AGREED_SUBMIT_DATE) 
    >=0;
    "YES";
    "NO"
)

i.e Date difference should be greater than or equal to zero. If so return yes else no.Please help me here.

like image 214
TheNightsWatch Avatar asked Aug 26 '14 12:08

TheNightsWatch


2 Answers

It would look something like this:

(case when project.PA_SUBMIT_DATE >= project.PA_AGREED_SUBMIT_DATE
      then 'YES' else 'NO'
 end)

Note: You can use >= for dates in both Excel and SQL and (I think) it makes the code easier to understand. The rest is just the standard SQL for a condition in a select.

like image 148
Gordon Linoff Avatar answered Oct 16 '22 10:10

Gordon Linoff


Looks like you want to return a "YES" if the PA_SUBMIT_DATE is greater than or equal to the PA_AGREED_SUBMIT_DATE:

SELECT CASE WHEN PA_SUBMIT_DATE >= PA_AGREED_SUBMIT_DATE 
          THEN 'YES' ELSE 'NO' 
       END AS [ColumnName]
FROM PROJECT
like image 3
T McKeown Avatar answered Oct 16 '22 09:10

T McKeown