truncate now() to only hour in mysql - 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.

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

How to query date in SQL for data in the last week

My current query is:
SELECT COUNT(*) FROM Previous_Appointment WHEREapDateBETWEEN '2017-04-03' AND '2017-04-27'
I need this to run however to just check today's date -7 days, -30 days, etc...
So that it returns the number of instances that have occured.
Try this,
Between today and today -7
SELECT COUNT(*) FROM Previous_Appointment WHERE DATE(apDate) > (NOW() - INTERVAL 7 DAY)
Between today and today -30
SELECT COUNT(*) FROM Previous_Appointment WHERE DATE(apDate) > (NOW() - INTERVAL 30 DAY)
you can use dateadd -7 days
dateadd(day,-7,cast(getdate() as date))
To get count in last week:
select count(*)
from previous_appointment
where apDate between curdate() - interval 7 day and curdate()
To get count in last 30 days:
select count(*)
from previous_appointment
where apDate between curdate() - interval 30 day and curdate()

SQL Search Midnight to Midnight

What would be in my where clause if I was trying to limit my results to yesterday, from midnight to midnight. I know ">= NOW() - 1 Interval Day" returns the past 24 hours, but for a daily report, and wanting to look at the report any time besides midnight itself, how can I accomplish this?
Thank you!
You can use CURDATE():
where col >= date_sub(CURDATE(), interval 1 day) and
col < CURDATE()
http://sqlfiddle.com/#!9/431a2/3
SELECT *
FROM t1
WHERE t >= CURDATE();
SELECT
CONCAT(DATE_SUB(CURDATE(), INTERVAL 1 DAY), ' 00:00:00') AS start_date,
CONCAT(DATE_SUB(CURDATE(), INTERVAL 1 DAY), ' 23:59:59') AS end_date;
That should get you the dates you want, then put the concat(...) parts into your where clause.

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