How can I select by date and hour

Asked

Viewed 61 times

0

Well what I intend initially is to do a while that shows me all the results of my column reserves. However I want to make a select that shows me the nearest days and then the nearest hours.

The name of my day column is "dialevant" and the name of my pick-up time is "horalevant", how can I make a select that shows me the nearest days and then the hours?

Thank you.

  • If dialevant will always store dates prior to or equal to the current date you can perform a SELECT in your table using "ORDER BY dialevant DESC, horalevant DESC" at the end of your query.

  • No, the dialevant, will only store future dates or current dates whose time has not yet passed the horalevant.

  • Can post the structure of your tables and how this bringing the data and how you want?

1 answer

0

If the day and time are separated, then join them with DATETIMEFROMPARTS. Its format is:

DATETIMEFROMPARTS ( year, month, day, hour, minute, seconds, milliseconds )

So try something like:

SELECT *
FROM reservas
WHERE DATETIMEFROMPARTS(
                       YEAR(dialevant),
                       MONTH(dialevant),
                       DAY(dialevant),
                       HOUR(horalevant),
                       MINUTE(horalevant),
                       0,
                       0) > DATETIME("SUA DATA AQUI")
ORDER BY DATETIMEFROMPARTS(
                       YEAR(dialevant),
                       MONTH(dialevant),
                       DAY(dialevant),
                       HOUR(horalevant),
                       MINUTE(horalevant),
                       0,
                       0) ASC

I hope it helps.

Browser other questions tagged

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