I have 2 time fields like hh:mm and I need to get the difference.
Example: The difference between 11:55 and 11:25 should be .30 or 30.
How can I achieve this?
It was in AM/PM format like 11:55 AM AND 11:25 AM. I have changed to normal time time using from_unixtime function but now unable to get difference.
Please try this:
select (unix_timestamp(end_time, 'HH:mm')
- unix_timestamp(start_time, 'HH:mm'))/60 from temp.test_table;
You should convert both dates to seconds and then substract them. Finally, if you want the result in minutes, divide it by 60.
Code Example:
create table temp.test_table (
start_time CHAR(5)
,end_time CHAR(5)
)
stored as parquet location '../temp.db/test_table';
INSERT INTO TABLE temp.test_table VALUES ('11:25', '11:55');
select (unix_timestamp(end_time, 'HH:mm')
- unix_timestamp(start_time, 'HH:mm'))/60 from temp.test_table;
Result: 30.0
Related
I'm currently looking at finding time differences between dates and outputting to hours (00:00). However, I'm then going on to sum this calculation which means I can't convert to char/varchar.
Please see example data below how I would like it to appear.
EMPLOYEE_ID Start End Duration
1 05/06/2015 08:30 05/06/2015 12:12 03:42
1 05/06/2015 13:18 05/06/2015 17:00 03:42
1 06/06/2015 08:30 06/06/2015 12:12 03:42
1 06/06/2015 13:18 06/06/2015 17:00 03:42
Current code for Duration:
CONVERT(varchar(5),DATEADD(minute,DATEDIFF(minute,Start, End),0), 114) as Duration
Obviously, this code is not how I want it as it's varchar and I'd like to go on to sum two durations together. I have looked around but can't find anything which shows how this would be done.
Calculate the duration in minutes:
select datediff(minute, start, end) as duration_min
. . .
This is much easier to work with. Then sum the minutes:
select sum(datediff(minute, start, end)) as duration_min
I would recommend leaving it like this. But if you want it in HH:MM format, then manually convert to a string of the format you want.
Don't use a time data type. It is limited to 24 hours.
SQL Server doesn't currently supoort the ANSI SQL INTERVAL data type so about the best you can do to support aggregation is calculate the duration in a consistent unit of measure (like minutes you used here) and then use the aggregation result to calculate the interval from a fixed datetime value. Although the example below uses the FORMAT function to convert the result to a string, I suggest formatting for display purposes be done in the presentation layer rather than T-SQL.
CREATE TABLE dbo.EmployTimeRecord (
EMPLOYEE_ID int NOT NULL
, StartTime smalldatetime NOT NULL
, EndTime smalldatetime NOT NULL
, DurationMinutes AS DATEDIFF(minute, StartTime, EndTime)
);
INSERT INTO dbo.EmployTimeRecord(EMPLOYEE_ID, StartTime, EndTime)
VALUES
(1, '2015-05-06T08:30:00', '2015-05-06T12:12:00')
, (1, '2015-05-06T13:18:00', '2015-05-06T17:00:00')
, (1, '2015-05-06T08:30:00', '2015-05-06T12:12:00')
, (1, '2015-05-06T13:18:00', '2015-05-06T17:00:00');
SELECT
EMPLOYEE_ID
, FORMAT(DATEADD(minute, SUM(DurationMinutes), ''), 'HH:mm') AS Duration
FROM dbo.EmployTimeRecord
GROUP BY EMPLOYEE_ID;
how to get exact time Difference between two column
eg:
col1 date is 2014-09-21 02:00:00
col2 date is 2014-09-22 01:00:00
output like
result: 23:00:00
I am getting result like
Hours Minutes Seconds
--------------------
3 3 20
1 2 30
using the following query
SELECT start_time,
end_time,
DATE_PART(H,end_time) - DATE_PART(H,start_time) AS Hours,
DATE_PART(M,end_time) - DATE_PART(M,start_time) AS Minutes,
DATE_PART(S,end_time) - DATE_PART(S,start_time) AS Seconds
FROM user_session
but i need like
Difference
-----------
03:03:20
01:02:30
Use DATEDIFF to get the seconds between the two datetimes:
DATEDIFF(second,'2014-09-23 00:00:00.000','2014-09-23 01:23:45.000')
Then use DATEADD to add the seconds to '1900-01-01 00:00:00':
DATEADD(seconds,5025,'1900-01-01 00:00:00')
Then CAST the result to a TIME data type (note that this limits you to 24 hours max):
CAST('1900-01-01 01:23:45' as TIME)
Then LTRIM the date part of the value off the TIME data (as discovered by Benny). Redshift does not allow use of TIME on actual stored data:
LTRIM('1900-01-01 01:23:45','1900-01-01')
Now, do it in a single step:
SELECT LTRIM(DATEADD(seconds,DATEDIFF(second,'2014-09-23 00:00:00','2014-09-23 01:23:45.000'),'1900-01-01 00:00:00'),'1900-01-01');
:)
SELECT LTRIM(DATEADD(seconds,DATEDIFF(second,'2014-09-23 00:00:00','2014-09-23 01:23:45.000'),'1900-01-01 00:00:00'),'1900-01-01');
I have a column with UTC Seconds in SQL.
How can I convert UTCSeconds to varchar in order to get this output in a select statement:
DD-MMM-YYYY HH:mm:ss
Example: 1400249277 in table
16-May-2014 15:07:57 as output of Select statement
I don't have a SQLServer available right now but this looks like it might be what you're looking for : DATEADD. You are probably considering the number of seconds since January the 1st 1970, so it would be something alike :
SELECT DATEADD (second, <your number of seconds>, '1970-01-01')
How to caculate sum of times of my colonne called "timeSpent" having this format: HH:mm
in SQL? I am using MySQL.
the type of my column is Time.
it has this structure
TimeFrom like 10:00:00 12:00:00 02:00:00
TimeUntil 08:00:00 09:15:00 01:15:00
Time spent
total time 03:15:00
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) ) AS timeSum
FROM YourTableName
100% working code to get sum of time out of MYSQL Database:
SELECT
SEC_TO_TIME( SUM(time_to_sec(`db`.`tablename`)))
As timeSum
FROM
`tablename`
Try and confirm.
Thanks.
In MySQL, you would do something like this to get the time interval:
SELECT TIMEDIFF('08:00:00', '10:00:00');
Then to add the time intervals, you would do:
SELECT ADDTIME('01:00:00', '01:30:00');
Unfortunately, you're not storing dates or using 24-hour time, so these calculations would end up incorrect since your TimeUntil is actually lower than your TimeFrom.
Another approach would be (assuming you sort out the above issue) to store the time intervals as seconds using TIMESTAMPDIFF():
UPDATE my_table SET time_spent=TIMESTAMPDIFF(start, end));
SELECT SEC_TO_TIME(SUM(time_spent)) FROM my_table;
If the data type of the timeSpent column is TIME you should be able to use the following query:
SELECT SUM(timeSpent)
FROM YourTableName -- replace YourTableName with the actual table name
However, if that is not the case, you may have to use a cast to convert to the Time data type. Something like this should work:
SELECT SUM(timeSpent - CAST('0:0:0' as TIME))
FROM YourTableName -- replace YourTableName with the actual table name
How to caculate sum of times of my colonne called "timeSpent" having this format: HH:mm
in SQL? I am using MySQL.
the type of my column is Time.
it has this structure
TimeFrom like 10:00:00 12:00:00 02:00:00
TimeUntil 08:00:00 09:15:00 01:15:00
Time spent
total time 03:15:00
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) ) AS timeSum
FROM YourTableName
100% working code to get sum of time out of MYSQL Database:
SELECT
SEC_TO_TIME( SUM(time_to_sec(`db`.`tablename`)))
As timeSum
FROM
`tablename`
Try and confirm.
Thanks.
In MySQL, you would do something like this to get the time interval:
SELECT TIMEDIFF('08:00:00', '10:00:00');
Then to add the time intervals, you would do:
SELECT ADDTIME('01:00:00', '01:30:00');
Unfortunately, you're not storing dates or using 24-hour time, so these calculations would end up incorrect since your TimeUntil is actually lower than your TimeFrom.
Another approach would be (assuming you sort out the above issue) to store the time intervals as seconds using TIMESTAMPDIFF():
UPDATE my_table SET time_spent=TIMESTAMPDIFF(start, end));
SELECT SEC_TO_TIME(SUM(time_spent)) FROM my_table;
If the data type of the timeSpent column is TIME you should be able to use the following query:
SELECT SUM(timeSpent)
FROM YourTableName -- replace YourTableName with the actual table name
However, if that is not the case, you may have to use a cast to convert to the Time data type. Something like this should work:
SELECT SUM(timeSpent - CAST('0:0:0' as TIME))
FROM YourTableName -- replace YourTableName with the actual table name