MySQL - SELECT rows between Yesterday 3pm and Today 7:30am - mysql

I'm trying to return rows between yesterday 15:00 and today 07:30, but can't seem to get it work.
I've tried the following two methods, but they haven't worked.
Note:
Getdate() should be CURDATE() in MySQL
TIMESERIAL is denied to my user account in the database
Code Examples:
where dateadd(dd, datediff(dd,0,Getdate()),0)- 1
and dateadd(dd, datediff(dd,0,Getdate()),0)- 1) + '23:59:59'
where [Table].[Date Time] Between Date()-1 + TimeSerial(18,0,0)
And Date() + TimeSerial(18,0,0)

you need to use the DATE_ADD and then INTERVAL n HOUR OR MINUTE.
Please check the attached fiddle. http://rextester.com/JIXU4144
select DATE_ADD(current_date, INTERVAL -9 HOUR);
select DATE_ADD(current_date, INTERVAL 450 minute);
select * from temp
where createdOn between '2018-08-28 15:00:00' and '2018-08-29 07:30:00';
select * from temp
where createdOn between DATE_ADD(current_date, INTERVAL -9 HOUR) and DATE_ADD(current_date, INTERVAL 450 minute)

If you are using MySQL, you can do:
where col >= curdate() - interval 1 day + interval 15 hour and
col < curdate() + interval 7.5 * 60 minute
You can also write this a bit more readably as:
where col >= curdate() - interval 1 day + interval 15 hour and
col < curdate() + interval '7:30' hour_minute

Related

I Want to Show only The Dates from a MySQL Database Between Today and the Next 30 Days

I have the following Code:
SELECT *
FROM `test_establishments`
WHERE expiry_date > CURDATE() AND expiry_date < CURDATE()+30
The Query returns 0 Rows and I do have dates in the database that expire within the next 30 days. I am not sure what the problem is with the code.
You can use :
select *
from `test_establishments`
where expiry_date between curdate() and curdate() + interval 30 day
Demo
You cannot use + on a datetime. You need to use DATE_ADD instead.
DATE_ADD(CURDATE(), interval 30 day)
Use this code 1 month or 30 days.
WHERE expiry_date > NOW() - INTERVAL 30 DAY
AND expiry_date < NOW() + INTERVAL 30 DAY

mysql where date is before now + 2 weeks

How to get the rows out of a MySQL DB where the field date is before NOW + 2 weeks?
I have tried
WHERE date_ready < DATE_SUB(CURDATE(), INTERVAL 2 WEEK)
But that is not the rows returning that I expect.
Or even, now() minus 2 week,
where date_ready < (NOW() - INTERVAL 2 WEEK)
with just date
where date_ready < (CURDATE() - INTERVAL 2 WEEK)
You're querying dates that are before today minus two weeks, not plus. You should use date_add instead of date_sub:
WHERE date_ready < DATE_ADD(CURDATE(), INTERVAL 2 WEEK)
-- Here -----------^
try this:
WHERE date_ready < DATE_ADD(now(), INTERVAL 2 WEEK)
-- Here -----------^
and
WHERE date_ready < DATE_ADD(CURDATE(), INTERVAL + 14 Day)
-- Here -----------^

how to use date and time interval using time stamp

sample time = '07:00:00'
Select date_add(sampletime, interval '24' HOUR), interval '20' minute) as samplefrom sample.time;
How could I use combination of hour and mins interval?
or
sample datetime = '2014/01/01 07:00:00'
SQL Query for date hour mins computation if possible.
Don't make it too complicated.
SELECT '2014-01-01 07:00:00' + INTERVAL 24 HOUR + INTERVAL 20 MINUTE;
or
SELECT '2014-01-01 07:00:00' + INTERVAL 1 DAY + INTERVAL 20 MINUTE;
If you insist on using the date_add() function, you forgot to use it twice.
SELECT DATE_ADD(DATE_ADD('2014-01-01 07:00:00', INTERVAL 24 HOUR), INTERVAL 20 MINUTE);

SQL Hourly Data

The query below retrieves weather data from a MySql database, and groups this data in to an hourly format.
select hour(datetime) AS hour
, avg(Temperature) as AVGT
from Database.minute
WHERE DATETIME
BETWEEN (CURDATE() + INTERVAL (SELECT hour(NOW())) hour - INTERVAL 23 hour)
AND ((CURDATE() + INTERVAL (SELECT hour(NOW())) hour))
group by hour
order by (CURDATE() + INTERVAL (SELECT hour(NOW())) hour - INTERVAL 23 hour)
Output is as follows:
hour AVGT
19 11.730
20 11.970
21 11.970
22 11.760
23 11.660
0 11.700
1 11.830
2 12.370
3 12.770
4 12.840
5 12.840
6 12.540
7 12.500
8 12.030
9 12.100
10 12.300
11 12.060
12 11.090
13 10.920
14 10.920
15 10.820
16 10.760
17 10.690
18 10.560
The time is now 18:15. All of the above output is correct apart from the data gathered for hour '18'. Instead of getting the average value between 18:00 and 18:15, it just outputs the average at time 18:00. ie. ignoring data between 18:01 and 18:14.
How can I modify the above query to include data in the current hour (18:00 to Now)?
Thanks
Why don't you simply try
SELECT Hour(datetime) AS hour,
Avg(temperature) AS AVGT
FROM DATABASE.minute
WHERE datetime BETWEEN ( Curdate() + INTERVAL (SELECT Hour(Now())) hour -
INTERVAL 23 hour ) AND Now()
GROUP BY hour
ORDER BY ( Curdate() + INTERVAL (SELECT Hour(Now())) hour - INTERVAL 23 hour )
I agree with #Ankur's answer (your filter citerion should not filter records up to the current hour, but rather the current time), however your date/time operations are very strange:
You don't need a subquery (SELECT Hour(NOW())) to obtain HOUR(NOW());
You can express ( Curdate() + INTERVAL (SELECT Hour(NOW())) hour - INTERVAL 23 hour ) more simply:
CURDATE() + INTERVAL HOUR(NOW()) - 23 HOUR
Or, in my view, more clearly:
DATE_FORMAT(NOW() - INTERVAL 23 HOUR, '%Y-%m-%d %H:00:00')
Your ORDER BY clause is a constant and therefore achieves nothing: did you mean to order by hour?
Therefore:
SELECT HOUR(datetime) AS hour,
AVG(Temperature) AS AVGT
FROM Database.minute
WHERE datetime BETWEEN
DATE_FORMAT(NOW() - INTERVAL 23 HOUR, '%Y-%m-%d %H:00:00')
AND NOW()
GROUP BY hour
ORDER BY hour

truncate now() to only hour in mysql

How do I truncate now() to get only upto hour precision in mysql?
In
select id from Run where submitTs <= now() and submitTs >= (now() - interval 1 hour);
I really want the records from 12.00 PM to 1 PM instead of 12.23 PM to 1.23 PM if I am running at 1.23 PM.
So how do I say
submitTs<= truncate_to_hour(now())
Try thsi query -
SELECT
id
FROM
Run
WHERE
DATE(submitTs) = CURDATE() AND
EXTRACT(HOUR FROM NOW()) = EXTRACT(HOUR FROM submitTs)
This query has better performance than first one -
SELECT
id
FROM
Run
WHERE
submitTs >= CURDATE() + INTERVAL EXTRACT(HOUR FROM NOW()) HOUR AND
submitTs < CURDATE() + INTERVAL EXTRACT(HOUR FROM NOW()) + 1 HOUR
something like this should work:
SELECT id
FROM Run
WHERE submitTs BETWEEN CAST(CONCAT(DATE_FORMAT(NOW(), '%Y-%m-%d %h'), ':00:00') AS DATETIME)
AND CAST(CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 HOUR), '%Y-%m-%d %h'), ':00:00') AS DATETIME);
I think some thing like this can help you to get hour of now():
HOUR(TIME(NOW())
Please see the reference at http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html to get more date-time function in mysql.