Check whether a date and time (timestamp) represents the current day

Asked

Viewed 2,825 times

9

I have a table with the following field: dh_send (TIMESTAMP) and I want to mount a query to get all records of this table in which the dh_send is equal to the current date (TODAY).

I thought as follows:

WHERE dh.envio BEETWEEN '2014-02-17 00:00:00' AND '2014-02-17 23:59:59'

Is there any way to improve? like using some Mysql constant? Because that way I need to get the date programmatically.

  • Go you get a date 23:59:59 and 50 hundredths. Ideally compare >= 00:00 and < 00:00 the next day. It’s also good to check how the bank handles the time zone issue.

3 answers

5


The alternative to doing this with pure SQL, without preparing the date before executing the querie is

WHERE DATE(dh.envio) = DATE(NOW())

But don’t do it. This automatic alternative has worse performance and should only be used if you are lazy and do not want to take advantage of query caching and are willing to do so by imagining that this query will calculate the date of ALL your table.

So, the way you are currently doing it is performative. Unless you have something against it, stay that way.

  • Thank you very much, that’s right. The query will run only one x on the day, with respect to the performance is very quiet.

0

I always use in language because it does not depend on the BD processing, in this case you can do as follows:

SELECT * FROM tabela WHERE dh.envio = (DIA ATUAL NA PROGRAMAÇÃO);

If it’s PHP for example:

   $sql="SELECT * FROM tabela WHERE dh.envio =". NOW();

I hope I’ve helped.

-1

WHERE DATE(dh.envio) = DATE(NOW())
  • 1

    Hello Felipe, can you add an explanation to this condition about why you think it solves the problem? So the answer is more complete.

Browser other questions tagged

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