Convert MySQL date and time to datetime - mysql

I have a table which has 4 columns. Date login, date logout, time login, time logout.
How can I get the difference. I tried something like this;
SUM(TIMESTAMP(date_logout, time_logout) - TIMESTAMP(date_log, time_log))
However I'm getting a really really high number and I am unable to convert it to proper time. Any hints or clues? I didn't design the table btw :P
Thanks in advance!

if you want the diff in secounds:
SUM(UNIX_TIMESTAMP(TIMESTAMP(date_logout, time_logout)) - UNIX_TIMESTAMP(TIMESTAMP(date_log, time_log)))
If you want the diff in days:
SUM(DATEDIFF(TIMESTAMP(date_logout, time_logout), TIMESTAMP(date_log, time_log)))
If you want the diff as HH:mm:ss
SUM(TIMEDIFF(TIMESTAMP(date_logout, time_logout), TIMESTAMP(date_log, time_log)))

Subtract the two value by using TIMEDIFF FUNCTION.
TIMEDIFF(TIMESTAMP(date_logout, time_logout) ,TIMESTAMP(date_log, time_log) )

TIMEDIFF(TIMESTAMP(date_logout, time_logout) , TIMESTAMP(date_log, time_log))

Related

MySQL timediff function does not give proper output

This function
SELECT HOUR(TIMEDIFF('2020-06-17 12:15:00am','2020-06-17 01:15:00am')) as 'diff'
gives me the difference as
11 hours
while actually it should be
1 hour
. How do I fix this? Please advice.
Thank you.
MySQL doesn't recognize am and pm by default, it parses times in 24-hour format. You need to use STR_TO_DATE() if you want to parse a custom datetime format.
Also, you need to put the later time first.
SELECT HOUR(TIMEDIFF(STR_TO_DATE('2020-06-17 01:15:00am', '%Y-%m-%d %r'),
STR_TO_DATE('2020-06-17 12:15:00am', '%Y-%m-%d %r'))) as 'diff'

Time calculation for time value entered in SQL

I need help on calculating my time difference. I search on the forum but not what I need. Here is the code I am using:-
(convert(varchar(10),([RT_Phase_Time])-(convert(time,'00:30:00'))))
So I [RT_Phase_Time] is in this format 'hh:mm:ss'. I am trying to get the difference between ([RT_Phase_Time] - '00:30:00'). Please help!
In MySQL you can easily do that with TIMEDIFF().
TIMEDIFF('22:00:00','00:30:00');
-- Returns '21:30:00'
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
Try something like this, check the BOL for datediff to convert to other values:
declare #rt_phase_time time
set #rt_phase_time = '12:30:30'
select datediff(ss,cast('00:30:00' as time),#rt_phase_time)

How to format time to '2:34 hrs', '0:34 hrs' etc in MySQL

I am writing a query that is selecting two times and calculating the difference between these, where one is the start and the other is the finish to find out the duration. However both are set to TIME types in mysql. How can i format the time instead of showing HH:MM:SS to '[OPTIONAL H]H':MM hrs'
Do i use the extract function?
I presume i have to wrap the TIMEDIFF around something else?
my sql code is as follows:
SELECT operator_no, log_in_time, TIMEDIFF(log_in_time,log_out_time) AS Duration
many thanks
Yes, wrap it in TIME_FORMAT():
SELECT operator_no, log_in_time,
TIME_FORMAT(
TIMEDIFF(log_in_time, log_out_time),
'%H:%i hrs'
) AS Duration
would this not work too?
SELECT operator_no, log_in_time,
CONCAT(EXTRACT(HOUR_MINUTE FROM TIMEDIFF (log_in_time,end_time)) + 'hrs' )
AS Duration

Difference in minutes from two time fields in MySQL

I set up my MySQL database with the field 'time'
It is not HH:MM in the traditional sense, it is the time an event occurred, so an event with the value of 5:45 occurred with 5 minutes 45 seconds left in a game. 12:25 occurred with 12 minutes and 25 seconds left, etc.
I would like to be able to find out the total time elapsed, so if I have an event that occurred at 12:25 and the next event occurred at 5:45 I want to be able to get the difference, which would be equal to 6:40. Then, I would like to express this as a decimal, in this case 6.67.
Is this possible?
For me this worked:
TIMESTAMPDIFF(MINUTE, T0.created, T0.modified)
Reference: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampdiff
Just use
TIMEDIFF(fromtime, totime):
SELECT TIMEDIFF("12:25", "5:45");
If you need a decimal, use TIME_TO_SEC()/3600 (it assumes you passed it seconds, but the format you're storing the times in is vague and SQL probably will interpret them as HH:MM - you can fix this with CONCAT("00:",MinuteSecondField) maybe?) - then you can use TIME_TO_SEC()/60, which is more correct)
I needed similar. Two useful functions: TIME_TO_SEC and SUBTIME.
e.g. if your time fields were normal HH:MM times, then
SELECT (TIME_TO_SEC(end_time) - TIME_TO_SEC(start_time))/60 AS `minutes`
In your case, as I understand it, the times are backwards, ie. 12:00<6:00 so your end_time and start_time would need swapping.
If you wanted the output in HH:MM:SS you could do
SELECT SUBTIME(end_time, start_time)
I used UNIX_TIMESTAMP(event1)-UNIX_TIMESTAMP(event2), which gives you seconds between events. Divide it by 60.0 and you will get a decimal as per your requirement. This solution assumes, your columns are date-compatible values. It will work really fast if they are TIMESTAMPs.
UPDATE: This solution only works with timestamp data types, not time datatypes as in original question.

mysql calculation

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in layman's term ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.
It sounds like you want this:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR)
...
This DATE_ADD function adds the given number of years to the starting date, thus producing the maturity date — just as you've described.
Please show the errors you get. The query
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) FROM capital
should work.