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.
Related
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')))
I have a table of MySQL data. Each row has its own data. In that row there is start_time and end_time. Basically, when you started doing an objective and when you finished (inserted into the database). Like a timer of sorts.
How would I get the average of of taking the unix timestamp of start_time and end_time. I know you would minus the end_time by start_time to get the difference (in milliseconds?) and from there... not sure what else.
unix_timestamp of a date column returns its representation as seconds from the epoc, so subtracting two of these will give a difference in seconds. Like any other number, you can apply the aggregate avg function to it in order to get an average:
SELECT AVG (UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time))
FROM my_table
Once you have this result, you could manipulate it in any way you like. One useful manipulation would be to use sec_to_time to convert a number of seconds to a HH:MM:SS format (e.g., 183 seconds would be represented as 00:03:03 hours):
SELECT SEC_TO_TIME
(AVG (UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time)))
FROM my_table
I have to make a SELECT from a table where the field with the time is in format: y-m-d h-m-s but I have to output the values that are entered for the current day between 00:00:01 and 23:59:59. My query looks like that:
SELECT user, insert_time FROM t_users
WHERE insert_time BETWEEN '2014-06-30 00-00-01' AND '2014-06-30 23-59-59'
The problem is that I don't have to hardcode the date... only the hours interval. And to make it better to understand the current problem I will make a wrong input what is needed to be done: 'TODAY 00-00-01' AND 'TODAY 23-59-59' which of course don't work but if there is a way to make a query that will output the today's added values, I will be grateful.
You can do this kind of calculation with PHP:
http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/
or MysQL solution:
http://blog.ubiq.co/difference-between-two-dates/
Also for today entries I think you can use:
SELECTid FROM my_table
WHERE
timestamp < date_format(date_add(CURRENT_TIMESTAMP(), interval 1 day),'%Y%m%d000000')
AND
timestamp >= date_format(CURRENT_TIMESTAMP(),'%Y%m%d000000')
I have a database table that has fields as such :
TIME(Datetime) Update_ID
2013-11-25 05:00:14 XC3
2013-11-25 06:00:13 XC4
2013-11-25 06:00:19 XC5
2013-12-25 23:00:14 XC6
2013-12-25 24:00:00 XC7
So assuming i want to find a trend on the updates to know which period of the day has the a particular number of updates, what i initially think of is doing something like this :
SELECT COUNT(TIME) FROM table WHERE TIME between '06:00:00' and '12:00:00'
But this doesn't work because i think since the date is not added with the time, a default value for date is added(some date around 1970). If, i add the beginning and enddate in my query, i am afraid it won't give me the results i need.
Use
WHERE HOUR(TIME)...GROUP BY DAY(TIME)
in case you have more than 1 day
You are correct, the problem is that when you do not specify the date, a default one is added.
You can use the EXTRACT function to extract the time from a date, like this:
SELECT COUNT(*)
FROM mytable
WHERE EXTRACT(HOUR_SECOND from TIME) between 60000 and 120000
Note that the time portion in the condition is specified in a different format - i.e. as numbers, without colons and quotes.
Demo on SqlFiddle.
I want to subtract between two date time values using SQL in MySQL such that I get the interval in minutes or seconds. Any ideas? I want to run a SQL query that retrieves uses from a database who have logged in like 10 minutes from the time.
There are functions TIMEDIFF(expr1,expr2), which returns the value of expr1-expr2, and TIME_TO_SEC(expr3), which expresses expr3 in seconds.
Note that expr1 and expr2 are datetime values, and expr3 is a time value only.
Check this link for more info.
TIMESTAMPDIFF is like TIMEDIFF which Matthew states, except it returns the difference of the two datetimes in whatever units you desire (seconds, minutes, hours, etc).
For example,
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
Would return the number of minutes the user was logged in (assuming you stored this kind of thing in a table like that).
I would do it like this - fetch where last activity is within 10 mins of now
SELECT * FROM table WHERE last_activity >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
This example shall ruin the time if its used by using millitary time. So for calculating millitairy time I do not recommend it Because it outputs negative values.
You can try and cast them to Unix Time stamp, and take the difference.