Date Difference in MySQL in Minutes - mysql

I need to find the time difference in Hours for the following Dates in MySQL - Can i use Datediff functions?
2014-01-01 07:27:21 and 2014-02-01 11:29:00
I tried using DATEDIFF(MINUTE,'2014-01-01 07:27:21','2014-01-01 11:29:00') but apparently MySQL is giving an error.

Time difference in minutes:
SELECT ROUND(TIME_TO_SEC(timediff(date1,date2))/60) AS diff
Example:
SELECT ROUND(TIME_TO_SEC(timediff('2014-01-01 11:29:00','2014-01-01 07:27:21'))/60) AS diff
Result:
242
Time difference in hours:
SELECT ROUND(TIME_TO_SEC(timediff(date1,date2))/60/60) AS diff
if you need number of hours with fractions then remove ROUND.

Related

MySQL WHERE condition to select all records from start of the hour

WHERE timestamp >= NOW() - INTERVAL 1 HOUR
The above query works fine to get data that belongs to last 1 HOUR. Let's say I run the query at 2020-10-15 18:08:00. It will give me all the records between the below time period.
2020-10-15 17:08:00 - 2020-10-15 18:08:00
I have been trying to create a mysql query to get data from the beginning of the hour. Let's again say I ran the query at 2020-10-15 18:08:00. I need a query to get me the results belonging the below time period.
2020-10-15 18:00:00 - 2020-10-15 18:08:00
I would appreciate some feedback on this.
In many databases (as Postgres for example), you can do this with date_trunc(), or the-like (in Oracle: trunc()).
Unfortunately MySQL does not implement this function - however it is flexible enough to understand dates as strings. I like to use date_format for this:
where timestamp >= date_format(now(), '%Y-%m-%d %H:00:00')

SQL - Time difference hh:mm:ss

My table looks something like this:
I want to subtract end_date from start_date and it should also subtract hh:mm:ss. The expected output for above should be 00:04:01
I tried multiple ways, but could not figure this out.
How can I do this? I am doing in MySQL
You can use timestampdiff() to compute the difference between both datetimes in seconds, and then sec_to_time() to turn the result to a time:
sec_to_time(timestampdiff(second, start_date, end_date))
Note that the time datatype stores values up to about 840 hours.

How to get half hour if exist with mysql hour function

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

MySQL - Subtracting time from a duration (NOT DateTime)

Specs: I'm using MySQL 5.6 with SQLWorkbench, SequelPro on OSX Yosemite
Query background: I'm trying to correct a set of TIMESTAMPDIFF durations for weekends and bank holidays. I have 2 stored procedures which are giving me the number of Saturdays, Sundays and Bank Holidays between two dates - these are working fine. To get the corrected TIMESTAMPDIFF, I therefore multiply the number of Saturdays, Sundays and holidays by 24 to get the number of hours to be subtracted, then subtract that number from the TIMESTAMPDIFF.
Example: As an example, if timestamp A is 14:00 on Friday and timestamp B is 14:01 on Tuesday, the raw TIMESTAMPDIFF is 96:01:00. Assuming Monday is holiday and the weekend is 48:00:00, I want to subtract 72:00:00 from 96:01:00, to get the 'business day difference' of 24:01:00.
The problem: When I do something like "96:01:00" - "72:00:00" as date_sub_test, I get 24. I have lost all formatting, including the 01 minute. Duration are not DATETIME, as they don't correspond to calendar dates, so I can't use DATE_ADD / DATE_SUB.
The question: How should I subtract time from a duration, retaining formatting and relevant base 60 system eg 60 minutes in an hour, not 100?
Thanks in advance!
As Jaydee mentions in a comment:
Have you tried the TIMEDIFF function? https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
TIMEDIFF was what I was looking for. I also added in ABS() to make negative time differences positive, and MAKETIME() to create a time from an integer.
Use SEC_TO_TIME and TIME_TO_SEC like
SELECT SEC_TO_TIME(TIME_TO_SEC('96:01:00')-TIME_TO_SEC('72:00:00'))
you can use ABS like this
SELECT SEC_TO_TIME(ABS(TIME_TO_SEC('96:01:00')-TIME_TO_SEC('172:00:00')))

mysql time comparison

I have job_start and job_end times and timediff will give me the time difference. Now I want to see if that job took more than 2 hrs 30 min. How do I compare it? I am getting errora if I do it like this:
timediff(job_start,job_end)> '2:30:00'
timediff(job_start,job_end)> time(2:30:00)
timediff(job_start,job_end)> time_format(2:30:00)
Nothing of the above syntax is working.
From mysql docs for function TIMESTAMPDIFF:
The unit for the result (an integer) is given by the unit argument.
The legal values for unit are the same as those listed in the description of the TIMESTAMPADD() function.
Which should be one of the following values: MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.
Result if integer.I recommend MINUTE:
TIMESTAMPDIFF(MINUTE, job_start, job_end) > 150
(2 * 60 + 30) mins = 150 mins = 2,5 hours