I have a MySQL query that uses two different timestamps that can be perceived as an interval of time. I need to implement an extra 25-30% of the difference. For example, if the difference between the times is 30 minutes, I need to request an additional 5 minutes before and 5 minutes after.. Is there a way to not only get the difference between the two time stamps, but then calculate this 'percentage' of time to acquire the appropriate interval, all within one statement?
Something like the following, where x is the calculated value.
..
DATE_FORMAT(timestamp1 - INTERVAL x MINUTE,'%m/%d/%Y %r') AS 'Start',
DATE_FORMAT(timestamp2 + INTERVAL x MINUTE,'%m/%d/%Y %r') AS 'End',
..
You have a start timestamp and an end timestamp in the database. You want to add an additional percentage of time to these values, based on the time span between them. In other words, you can't add or subtract any amount of time without knowing the difference between the two.
So, first you must figure out the time span. I'm going to use minutes as the unit, but use whatever works best for your needs:
SELECT
#new_span := TIMESTAMPDIFF(MINUTE, timestamp1, timestamp2) * 1.25 new_span,
DATE_SUB(timestamp1, INTERVAL (#new_span*60)/2 SECOND) AS new_start,
DATE_ADD(timestamp2, INTERVAL (#new_span*60)/2 SECOND) AS new_end,
TIMESTAMPDIFF(MINUTE, timestamp1, timestamp2) old_span,
timestamp1 AS previous_start,
timestamp2 AS previous_end
FROM table;
So this query stores the difference between the two times plus 25% as a variable named new_span. The new_start and new_end fields use the variable (converted to seconds and divided in half) to modify the original start/end times. I'm also selecting old_span and the original start/end times, for comparison.
Here is sample output:
new_span new_start new_end old_span previous_start previous_end
1.25 2011-08-11 15:53:22 2011-08-11 15:55:38 1 2011-08-11 15:54:00 2011-08-11 15:55:00
1350.00 2011-08-09 00:45:00 2011-08-10 17:15:00 1080 2011-08-09 12:00:00 2011-08-10 06:00:00
Related
I want to get 8:30 hour instead of 8 from below query
HOUR(TIMEDIFF('2018-12-01 07:00:00','2018-12-01 15:30:00')
Mysql hour function only return number of hour not half hour
We can get the difference between two datetime values in SECOND units, using TimeStampDiff() function. Now, we can convert this into Time using Sec_To_time() function.
SELECT SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2018-12-01 07:00:00','2018-12-01 15:30:00'))
Result
| SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2018-12-01 07:00:00','2018-12-01 15:30:00')) |
| ------------------------------------------------------------------------------- |
| 08:30:00 |
You may call both HOUR and MINUTE, the latter to get the minute component:
SELECT
HOUR(TIMEDIFF('2018-12-01 07:00:00','2018-12-01 15:30:00')) +
MINUTE(TIMEDIFF('2018-12-01 07:00:00','2018-12-01 15:30:00')) / 60.0 AS hours
FROM yourTable;
This outputs 8.5 for the number of hours.
Another option would be to first convert both timestamps to UNIX timestamps in seconds since epoch. Then convert their difference back to hours:
SELECT
(UNIX_TIMESTAMP('2018-12-01 15:30:00') -
UNIX_TIMESTAMP('2018-12-01 07:00:00')) / 3600.0 AS hours
FROM yourTable;
Demo
So let's say that I want to keep track of my CPU temperature, with simple columns 'date' and 'temperature'. I'd like to see what period saw the highest temperatures on average in the last week. I capture the data every 10 minutes, so I want each 10 minute block averaged with the same block from the other days of the week.
So for example:
2018-01-08 02:00:00 78.3
2018-01-08 03:00:00 81.2
2018-01-09 02:00:00 74.1
2018-01-09 03:00:00 75.9
I would want the averages of each day # 02:00:00, each day # 03:00:00, and so on. (except the real data is every 10 minutes) The exact datetime varies - it's not always 02:00:02, sometimes it could be 02:00:07, etc., so I can't just do an exact conditional.
Any idea how I'd go about making this data? I assume there's some way I can use GROUP BY for this, but I'm lost as to how.
Format just the hour and minute, and group by that.
SELECT DATE_FORMAT(date, '%H:%i') AS time, AVG(temperature) AS temp
FROM yourTable
GROUP BY time
This assumes that the readings are never delayed by more than a minute from the expected 10-minute periods -- if the reading for 02:10 happens at 02:11:01 it will not be put in the group.
I have a column in a table of type TIME. I want to get a result that applies a time shift that results in a 24 hour clock representation of that shift. To add the shift, my query contains...
select addtime(boo,'01:00:00') as yada
But any value that gets taken out of the 24 hour range ends up outside the 24 hour range, such as...
23:45 ends up as 24:45 (when I want 00:45:00)
If I go the other way and subtract the hour from a value less than 1am, I get...
00:15 ends up as -00:45:00 when I want (23:15:00)
Now, I understand that the TIME format in MYSQL is "duration" and not the actual "time", but for the life of me I can't figure out how to convert to an actual clock time as outlined above. Please help me or kill me. Either will end my suffering.
A simple solution would be to just use a DATETIME data type instead, then ignore the date part. If you're not dealing with huge amounts of data, or searching by the actual times I can't see an issue.
As a bonus you'll be able to manipulate the data with the likes of + INTERVAL 1 HOUR etc.
When extracting it just use TIME(boo)
As you know, the MySQL TIME type is not restricted to 24 hour time so this is probably the closest you'll get... I'm sure you could construct a query using MOD() etc but it's probably not worth it.
A possible solution is just add your boo TIME value to any date (e.g. today) then add your time delta and after that just return time part with TIME()
SELECT TIME(CURDATE()
+ INTERVAL TIME_TO_SEC('23:45:00') SECOND
+ INTERVAL 1 HOUR) new_time
Output:
+----------+
| new_time |
+----------+
| 00:45:00 |
+----------+
Here is SQLFiddle demo
I have a date time field in a MySQL database and wish to output the result to the nearest hour.
e.g. 2012-04-01 00:00:01 should read 2012-04-01 00:00:00
Update: I think https://stackoverflow.com/a/21330407/480943 is a better answer.
You can do it with some date arithmetic:
SELECT some_columns,
DATE_ADD(
DATE_FORMAT(the_date, "%Y-%m-%d %H:00:00"),
INTERVAL IF(MINUTE(the_date) < 30, 0, 1) HOUR
) AS the_rounded_date
FROM your_table
Explanations:
DATE_FORMAT: DATE_FORMAT(the_date, "%Y-%m-%d %H:00:00") returns the date truncated down to the nearest hour (sets the minute and second parts to zero).
MINUTE: MINUTE(the_date) gets the minute value of the date.
IF: This is a conditional; if the value in parameter 1 is true, then it returns parameter 2, otherwise it returns parameter 3. So IF(MINUTE(the_date) < 30, 0, 1) means "If the minute value is less than 30, return 0, otherwise return 1". This is what we're going to use to round -- it's the number of hours to add back on.
DATE_ADD: This adds the number of hours for the round into the result.
Half of the hour is a 30 minutes. Simply add 30 minutes to timestamp and truncate minutes and seconds.
SELECT DATE_FORMAT(DATE_ADD(timestamp_column, INTERVAL 30 MINUTE),'%Y-%m-%d %H:00:00') FROM table
soul's first solution truncates instead of rounding and the second solution doesn't work with Daylight Savings cases such as:
select FROM_UNIXTIME(UNIX_TIMESTAMP('2012-03-11 2:14:00') - MOD(UNIX_TIMESTAMP('2012-03-11 2:14:00'),300));
Here is an alternate method (1):
DATE_ADD(
tick,
INTERVAL (IF((MINUTE(tick)*60)+SECOND(tick) < 1800, 0, 3600) - (MINUTE(tick)*60)+SECOND(tick)) SECOND
)
If you don't need to worry about seconds you can simplify it like this (2):
DATE_ADD(
tick,
INTERVAL (IF(MINUTE(tick) < 30, 0, 60) - MINUTE(tick)) MINUTE
)
Or if you prefer to truncate instead of round, here is simpler version of soul's method (3):
DATE_SUB(tick, INTERVAL MINUTE(tick)*60+SECOND(tick) SECOND)
EDIT: I profiled some of these queries on my local machine and found that for 100,000 rows the average times were as follows:
soul's UNIXTIME method: 0.0423 ms (fast, but doesn't work with DST)
My method 3: 0.1255 ms
My method 2: 0.1289 ms
Ben Lee's DATE_FORMAT method: 0.1495 ms
My method 1: 0.1506 ms
From How to round a DateTime in MySQL?:
It's a little nasty when you do it with datetime data types; a nice candidate for a stored function.
DATE_SUB(DATE_SUB(time, INTERVAL MOD(MINUTE(time),5) MINUTE ),
INTERVAL SECOND(time) SECOND)
It's easier when you use UNIXTIME timestamps but that's limited to a 1970 - 2038 date range.
FROM_UNIXTIME(UNIX_TIMESTAMP(time) - MOD(UNIX_TIMESTAMP(time),300))
Good luck.
To round down to the current hour, select:
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(column_name) / 3600) * 3600).
The value is expressed in the current time zone doc
This will return the next hour, that is '2012-01-02 18:02:30' will be converted into '2012-01-02 19:00:00'
TIMESTAMPADD(HOUR,
TIMESTAMPDIFF(HOUR,CURDATE(),timestamp_column_name),
CURDATE())
Instead of CURDATE() you can use an arbitrary date, for example '2000-01-01'
Not sure if there could be problems using CURDATE() if the system date changes between the two calls to the function, don't know if Mysql would call both at the same time.
to get the nearest hour would be:
TIMESTAMPADD(MINUTE,
ROUND(TIMESTAMPDIFF(MINUTE,CURDATE(),timestamp_column_name)/60)*60,
CURDATE())
changing 60 by 15 would get the nearest 15 minutes interval, using SECOND you can get the nearest desired second interval, etc.
To get the previous hour use TRUNCATE() or FLOOR() instead of ROUND().
Hope this helps.
If you need to round just time to next hour you may use this:
SELECT TIME_FORMAT(
ADDTIME(
TIMEDIFF('16:15', '10:00'), '00:59:00'
),
'%H:00:00'
)
I think this is the best way, since it also will use the least amount of resources-
date_add(date(date_completed), interval hour(date_completed) hour) as date_hr
How can I subtract time in MySQL? For example, today is 16 March; I want to subtract 15 days to reach 1 March. Are there any methods that can be used to subtract 15 days from the current date?
SELECT DATE(NOW()-INTERVAL 15 DAY)
For a list of units see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
Not entirely related to this question but is related to the title:
SELECT SUBTIME("10:24:21", "5"); -- subtracts 5 seconds. (returns "10:24:16")
SELECT SUBTIME("10:24:21", "01:00:00"); -- subtracts one hour. (returns "09:24:21")
Documentation: MySQL SUBTIME function
Use:
SELECT NOW() - INTERVAL 15 DAY
to keep the datetime precision.
You can use this :
SELECT DATE(NOW()-INTERVAL 15 DAY);
for when you want to subtract the number of days.
In order to subtract the time instead, say 15 minutes, the following should work:
SELECT(DATE_SUB(NOW(), INTERVAL '15:0' MINUTE_SECOND));
Adding the reference link again :- https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add.
Yes its possible using date function in Mysql
select distinct
lastname,
changedat, date_add(changedat, interval -15 day) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.
I have subtract 15 days from my date - check image please. thanks