select specific time from previous day - mysql

I basically want to select data between 7pm the previous day and NOW(). I'm not sure the best practice or most efficient way to do this on an automated report generated by a query I could write.
SELECT * FROM table WHERE timestamp BETWEEN curdate() - INTERVAL 1 DAY AND
NOW()
How do I get the curdate() - interval 1 day to start at 7pm of the previous day?

curdate() returns just the date, e.g. 2018-03-21. Time in this case is omitted, but would be 00:00:00. To start at 07:00 pm just add another 19 hours like this:
select curdate() - INTERVAL 1 DAY + INTERVAL 19 HOUR;

Related

get mysql record older than one month only

How to get mysql record older than 30 days? my code will get all the records even which are inserted two months ago .
WHERE date < DATE_SUB(NOW(), INTERVAL 1 MONTH)
I want only one month ago not bigger than one month
Put both start and end date in the filter.
WHERE date >= CURDATE() - INVERVAL 2 MONTH
AND date < CURDATE() - INTERVAL 1 MONTH
It's verbose and repetitive, but that's an affliction of all SQL code.
Calendar months? If you, on May 7th or anytime in May, want to ask for the calendar month of April, it would be this
WHERE date >= LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 2 MONTH
AND date < LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
LAST_DAY('2021-05-07') gets you '2021-05-31',
+ INTERVAL 1 DAY then gets you '2021-06-01', then
- INTERVAL 2 MONTH finally gets you '2021-04-01'
It's easy to read and reason about.
CURDATE() gives today's date in place of the current date and time given by NOW(). Lots of historical reporting doesn't care about time of day, just calendar day. So it might be smart to use CURDATE(), depending on your application.

date condition to retrieve data in a 24 hour time window mysql

I am using perl and DBI to query a mysql table. I need to retrieve all rows (aprox. 75,000 rows from 3 separate databases) within the past 24 hours, ideally between 12:00 am and 11:59 pm or 00:00:00 and 23:59:59.
I was using a WHERE date condition like this:
SELECT *
FROM table_name
WHERE insert_date >= DATE_SUB(NOW(), INTERVAL 1 DAY);
Then I would run my script at midnight using cron. This worked well enough, but due to a regular large volume of traffic at midnight and the size of the queries, the execution time scheduled with cron is now 3:00 am. I changed my sql to try and get the same 24 hour period from an offset like this:
SELECT *
FROM table_name
WHERE insert_date
BETWEEN DATE_SUB(DATE_SUB(NOW(), INTERVAL 3 HOUR), INTERVAL 1 DAY)
AND DATE_SUB(NOW(), INTERVAL 3 HOUR);
This seems to work fine for my purposes but I want to ask, is there is a more readable and more accurate way, using mysql, to get all rows from the past 24 hours ( between 00:00:00 and 23:59:59 time window ) once a day while running the query from an offset time? I am generally new to all of this so any critiques on my overall approach are more than welcome.
I presume insert_date is a DATETIME?
It seems pointless to go to all the trouble of building two limits and using BETWEEN. I would simply check that DATE(insert_date) is yesterday's date. So
WHERE DATE(insert_date) = CURDATE() - INTERVAL 1 DAY
BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 DAY), "%Y-%M-%d 00:00:00")
AND DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 DAY), "%Y-%M-%d 23:59:59")
You could also use Perl date formatting functions to produce the same date-time strings, and interpolate them into the query.
....
WHERE insert_date BETWEEN CURRENT_DATE - INTERVAL 1 DAY
AND
CURRENT_DATE - INTERVAL 1 SECOND;
The lower bound will be coerced to yesterday's YYYY-MM-DD 00:00:00, and this WHERE predicate will be able to make use of an index on insert_date.
Considering that DATE(NOW()) implicitly means midnight this morning, the obvious solution is to take that value and subtract a day for the start... and subtract a second for the end.
BETWEEN DATE_SUB(DATE(NOW()), INTERVAL 1 DAY)
AND DATE_SUB(DATE(NOW()), INTERVAL 1 SECOND)

How to subtract 30 days from the current datetime in mysql?

How do I subtract 30 days from the current datetime in mysql?
SELECT * FROM table
WHERE exec_datetime BETWEEN DATEDIFF(NOW() - 30 days) AND NOW();
SELECT * FROM table
WHERE exec_datetime BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW();
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
To anyone who doesn't want to use DATE_SUB, use CURRENT_DATE:
SELECT CURRENT_DATE - INTERVAL 30 DAY
MySQL subtract days from now:
select now(), now() - interval 1 day
Prints:
2014-10-08 09:00:56 2014-10-07 09:00:56
Other Interval Temporal Expression Unit arguments:
https://dev.mysql.com/doc/refman/5.5/en/expressions.html#temporal-intervals
select now() - interval 1 microsecond
select now() - interval 1 second
select now() - interval 1 minute
select now() - interval 1 hour
select now() - interval 1 day
select now() - interval 1 week
select now() - interval 1 month
select now() - interval 1 year
Let's not use NOW() as you're losing any query caching or optimization because the query is different every time. See the list of functions you should not use in the MySQL documentation.
In the code below, let's assume this table is growing with time. New stuff is added and you want to show just the stuff in the last 30 days. This is the most common case.
Note that the date has been added as a string. It is better to add the date in this way, from your calling code, than to use the NOW() function as it kills your caching.
SELECT * FROM table WHERE exec_datetime >= DATE_SUB('2012-06-12', INTERVAL 30 DAY);
You can use BETWEEN if you really just want stuff from this very second to 30 days before this very second, but that's not a common use case in my experience, so I hope the simplified query can serve you well.
You can also use
select CURDATE()-INTERVAL 30 DAY
SELECT date_format(current_date - INTERVAL 50 DAY,'%d-%b-%Y')
You can format by using date format in SQL.
If you only need the date and not the time use:
select*from table where exec_datetime
between subdate(curdate(), 30)and curdate();
Since curdate() omits the time component, it's potentially faster than now() and more "semantically correct" in cases where you're only interested in the date.
Also, subdate()'s 2-arity overload is potentially faster than using interval.
interval is meant to be for cases when you need a non-day component.
another way
SELECT COUNT(*) FROM tbl_debug WHERE TO_DAYS(`when`) < TO_DAYS(NOW())-30 ;

Select records from NOW() -1 Day

Is there a way in a MySQL statement to order records (through a date stamp) by >= NOW() -1 so all records from the day before today to the future are selected?
Judging by the documentation for date/time functions, you should be able to do something like:
SELECT * FROM FOO
WHERE MY_DATE_FIELD >= NOW() - INTERVAL 1 DAY
Be aware that the result may be slightly different than you expect.
NOW() returns a DATETIME.
And INTERVAL works as named, e.g. INTERVAL 1 DAY = 24 hours.
So if your script is cron'd to run at 03:00, it will miss the first three hours of records from the 'oldest' day.
To get the whole day use CURDATE() - INTERVAL 1 DAY. This will get back to the beginning of the previous day regardless of when the script is run.
Didn't see any answers correctly using DATE_ADD or DATE_SUB:
Subtract 1 day from NOW()
...WHERE DATE_FIELD >= DATE_SUB(NOW(), INTERVAL 1 DAY)
Add 1 day from NOW()
...WHERE DATE_FIELD >= DATE_ADD(NOW(), INTERVAL 1 DAY)
You're almost there: it's NOW() - INTERVAL 1 DAY
Sure you can:
SELECT * FROM table
WHERE DateStamp > DATE_ADD(NOW(), INTERVAL -1 DAY)
when search field is timestamp and you want find records from 0 hours yesterday and 0 hour today use construction
MY_DATE_TIME_FIELD between makedate(year(now()), date_format(now(),'%j')-1) and makedate(year(now()), date_format(now(),'%j'))
instead
now() - interval 1 day

How to get dates which are between last monday till current day?

I am having dates in my database.
My database is in MySQL.
I want to fetch dates from my database which provides me dates from last monday till current day.
How can I do that?
You first have to work out how many days ago last monday was, using the DAYOFWEEK function, then subtract that from the current date -
SELECT * from table
WHERE date >= DATE_SUB(CURDATE(),INTERVAL MOD(DAYOFWEEK(CURDATE())-2,7) DAY)
AND date <= DATE_ADD(CURDATE(), INTERVAL MOD(7 - (DAYOFWEEK(CURDATE()) - 1), 7) DAY)
I'm not 100% sure about the +/- numbers here, you should be able to work it out from this though
EDIT: If this will only ever be run on the sunday at the end of the period, there is a much simpler version -
SELECT * from table
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 6 DAY)
AND date <= CURDATE()
try this one
select * from table
WHERE date >date_sub(curdate(), interval WEEKDAY(curdate()) day) ;
You could always use the between function in your queries...
SELECT *
FROM orders
WHERE order_date between to_date ('2003/01/01', 'yyyy/mm/dd')
AND to_date ('2003/12/31', 'yyyy/mm/dd');
http://www.techonthenet.com/sql/between.php