Home > Back-end >  sql query to check for expiration of event, which checks for both date and time in two separate colu
sql query to check for expiration of event, which checks for both date and time in two separate colu

Time:12-22

We have a database for events, expiration_date and expiration_time each having a separate column. Currently, we check if the expiration date is less than the current date and then we don't show those events to the users. The problem we're currently having is if the event gets finished after 12 am and above the event still gets shown for the entire day.

so is there a way I can write a query that checks for expiration_time only if the expiration_date==Current_Date?

CodePudding user response:

Use OR to combine the two cases.

SELECT columns
FROM yourTable
WHERE expiration_date > CURRENT_DATE() OR 
    (expiration_date = CURRENT_DATE() AND expiration_time >= CURRENT_TIME())

Or you can combine the date and time and compare them to NOW()

WHERE TIMESTAMP(expiration_date, expiration_time) >= NOW()
  • Related