MySQL - Subtracting time from a duration (NOT DateTime) - mysql

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

Related

How many Intervals are in a period?

Given a table with two dates and a date interval (i.e. 6 WEEKS), how can I count how many times this interval fits in this date interval?
In my PHP script I'm using a simple for loop, but I'd like to add this in a MySQL query.
A simple calculation would be to count the number of days between the dates and then assume a month has 30 days and a year has 365 days, but that's not always true.
How can I calculate with datediff and a date interval string?
When I understood you correctly, you need the timestampdiff() function.
The first parameter is the unit of the interval. The result is how many units are between the two dates.

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.

MySQL Finding Number of Elapsed Weeks in Current Month

QUESTION
Is it possible the find the number of weeks in the current month using a MySQL query?
If you answer the above question, then you have answered my question. However, I have been downvoted before for not including context, so....
BACKGROUND (Warning: It Will Confuse You)
I advise not reading any further
Because of a MySQL parser we have written in an application, my hands are tied with the way in which I must approach writing queries.
Here's the scenario:
I need to average out the number of hours worked per week over the course of a month by an employee; however, I must approach this by generating a column returned by the query that can then be summed to give me the average (yes, that is a mouth full).
Here is what I mean (and one attempt):
SELECT ets.*, (ets.hours/4) AS AVG_HOURS
FROM employee_timesheet AS ets
WHERE ets.date_worked >= (NOW() - INTERVAL 1 MONTH)
Yes, the WHERE clause is not accurate, that will have to change, too (but that is the next step).
The application then SUMS(avg_hours). Consider that each ets record contains the hours worked per day, by dividing the (hours worked each day) / by the (floating representation of the number of weeks in the month), I should then get the average worked per week over the course of the month by summing the result of the division.
To represent the logic in pseudo-code:
SUM(days_hours/number_of_weeks)
You can use DATEDIFF function
SELECT DATEDIFF('2014-10-30','2014-12-29') AS DiffDate
it should return the days between two date
https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
To answer the question, yes you can find the number of weeks from a month by using SQL's
datepart(week, date).
If you need the current month's number of weeks you can do
Datepart(week, getdate())
Here's more information for DATEPART
To get the first day of the month you can use
DATEADD(month, DATEDIFF(month, 0, #mydate), 0)
Alright, I got it. If I want to get the current number of weeks elapsed in the current month, I can:
DAYOFMONTH(CURRENTDATE()) / 7

Date Difference in MySQL in Minutes

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.

mysql date bug because of subtracting one day

I tried to do the following query to subtract one day from the given date:
DATE(FROM_UNIXTIME(UNIX_TIMESTAMP('2013-04-01') - 86400))
which returns me 2013-03-30, but it should return 2013-03-31.
If i try to subtract one day of 2013-04-02, i get 2013-04-01 correctly returned.
Is this a date bug in mysql?
It's not a bug - what you have found is the missing hour in daylight saving time: a thing unixtime and your calculation is unaware of since you calculate with seconds and not days.
This is exactly why DBMS have special DATETIME datatypes - to handle all the specialities in timezones, leap years, leap seconds, daylight savings and calendars.
Let the database do the work for you - here is an easier and better way to get what you want:
SELECT DATE('2013-04-01') - INTERVAL 1 DAY
Your code makes the assumption that all days have 24 hours. Yesterday, 31st March, had 23 hours in most European countries. To subtract one day you need something like this:
SELECT NOW() - INTERVAL 1 DAY
Why not use the built in DATE_SUB() function?
SELECT DATE_SUB('2013-04-01', INTERVAL 1 DAY)
EDIT: No, This is NOT a date bug in MySQL.