how to use date and time interval using time stamp - mysql

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);

Related

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

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

SQL: Show "running" events

my script shows the events of the next 14 days:
SELECT
*
FROM
events
WHERE
eventdate
BETWEEN CURDATE() AND DATE_ADD(NOW(), INTERVAL 14 DAY)
This works fine so far BUT at midnight all "running" (night)events are not displayed anymore.
So the query should output the events from the day before till 5 a.m.
This is my idea but it doesn't work:
SELECT
*
FROM
events
WHERE
eventdate
BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 HOUR)
AND
DATE_ADD(NOW(), INTERVAL 14 DAY)
Is it because 'eventdate' is a DATE-field?
Thanks!
It sounds like you want to subtract 5 hours before doing the comparison:
WHERE date_sub(eventdate, interval 5 hour) BETWEEN CURDATE() AND DATE_ADD(NOW(), INTERVAL 14 DAY)
Alternatively, you can add 5 hours on the other side:
where eventdate between now() and datea_add(now(), interval 14*24 + 5 hour)
This should do the job or not!?
WHERE
eventdate
BETWEEN DATE(DATE_SUB(NOW(), INTERVAL 5 HOUR))
AND
DATE_ADD(NOW(), INTERVAL 14 DAY)

Confusion about using a where clause, date interval, and 'and' and or 'or', should be quick

SELECT *
FROM romantic_dates
WHERE
asker = 'lover' AND date_start BETWEEN NOW() + INTERVAL 90 DAY AND NOW() - INTERVAL 31 DAY
OR
giver = 'lover' AND date_start BETWEEN NOW() + INTERVAL 90 DAY AND NOW() - INTERVAL 31 DAY
ORDER BY date_start DESC;
This is a where clause I am building. I am confused on just how to use the 'or' and between now() and interval. Anyone available to help? This question is slightly different then the others I searched for in that there are docs on one or the other on and off stackoverflow, but not dealing with both.
The operands for between are ordered, so the first needs to be less than the second. That is not the case with your expression. So:
SELECT rd.*
FROM romantic_dates rd
WHERE (asker = 'lover' AND date_start BETWEEN NOW() - INTERVAL 31 DAY AND NOW() + INTERVAL 90 DAY) OR
(giver = 'lover' AND date_start BETWEEN NOW() - INTERVAL 31 DAY AND NOW() + INTERVAL 90 DAY)
ORDER BY date_start DESC;
I would be inclined to factor out the date expression and use:
SELECT rd.*
FROM romantic_dates rd
WHERE date_start BETWEEN NOW() - INTERVAL 31 DAY AND NOW() + INTERVAL 90 DAY AND
'lover' in (asker, giver)
ORDER BY date_start DESC;

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.