I want to compare in Mysql "IF" or "CASE"

Asked

Viewed 403 times

-1

I just want to do a comparison of dates in Mysql but I can’t. Help me, it’s to see if the date is greater than or equal to a date, and less than another date.

SELECT
    (CASE 
        WHEN '2019/12/05 00:00:00' >= age_start THEN 'existe' 
        WHEN '2019/12/05 00:00:00'<= age_end 
        ELSE 'okay' 
    END)
FROM agenda

After researching I arrived at this code but it is not certain, why?

  • It would not be simpler something like: SELECT * FROM agenda WSHERE age_start <= '2019/12/05 00:00:00' AND age_end >= '2019/12/05 00:00:00'; ?

1 answer

1

You forgot to put the second THEN, and jumped right into the ELSE:

SELECT
   (CASE WHEN '2019/12/05 00:00:00' >= age_start THEN 'existe' 
         WHEN '2019/12/05 00:00:00'<= age_end THEN 'Menor igual a age_end' -- <=== Faltou essa parte
         ELSE 'okay' END
   )FROM agenda

But if you simply want to return "okay" if you are within the period, you should try it like this:

SELECT
   (CASE WHEN '2019/12/05 00:00:00' BETWEEN age_start AND age_end THEN 'Okay' 
         ELSE 'Not okay' END
   )FROM agenda

Browser other questions tagged

You are not signed in. Login or sign up in order to post.