DATEDIFF in dd,hh,mm,ss in Impala - sql

I want a query that shows a time difference in hours and minutes in Impala. Or in SQL?
How can I do this?
start 2017-11-29 19:45:00 - end 2017-11-29 21:30:00

So DATEDIFF gives you the difference between two dates in DAYS. This can be converted to hours by multiplying by 24. The remainder of this answer is then minutes if multiplied by 60.

Try use
secondsDiff = timestamp2 - timestamp1
minutes_diff = secondsDiff / 60
hours_diff = secondsDiff / (60*60)

Related

Difference in datetime stamps impala

I want a query that shows a time difference in months or days in Impala
How can I do this?
start 2017-11-29 19:45:00 - end 2018-11-29 21:30:00
I know that month_between and datediff shows the month of datediff but how do I make it so it also takes the year into count when counting the days / months?
For the above example, I want to to display either
month_between - 12.2 months - equivalent to the month calculation of the timestamp - might be a little off cause I did it by hand / 30 days
days_between - 366 days
not sure if you tried DATEDIFF or not , but it already gives you the dates difference in days :
select datediff(endddaate, startdate)
from tablename

Calculate time difference in PSQL with HH:MM splitted in several columns

I've a PSQL table like this:
Order
Start_Hour
Start_Minute
Finish_Hour
Finish_Minute
10
10
15
12
15
10
12
15
14
15
10
16
00
17
00
And I need to calculate by a query the total time expressed in hours that I spent to finish the order. In this scenario I expect to have a total of 5 hours:
12:15 - 10:15 = 2 hours
14:15 - 12:15 = 2 hours
17:00 - 16:00 = 1 hours
The query result must be 5.
The idea was concatenate start hour/minute and finish hour/minute, convert them to hour, make the difference, calculating the total.
SELECT (Start_Hour & ":" & Start_Minute) as start, (Finish_Hour & ":" & Finish_Minute) as finish
FROM OrderDetails
But when I try to convert them to HH:MM using cast or convert but I got errors.
Any advice?
Thank you
This query uses make_time as Adrian Klaver suggests.
select
"Order",
sum(extract(hour from
make_time("Finish_Hour", "Finish_Minute", 0) -
make_time("Start_Hour", "Start_Minute", 0))
) as duration
from the_table
group by "Order";
However I have remarks about your data design. Hour and minute are not enough for storing time because (apart from missing precision and other reasons) the end time might be over midnight. You have a specific data type for this - timestamp. I would suggest something like
create table the_table
(
order_nr integer,
start_time timestamp,
finish_time timestamp
);
Also note that using mixed case names in Postgresql requires double-quoting.
Use make_time:
select make_time(12, 15, 0) - make_time(10, 15, 0);
?column?
----------
02:00:00
Where in your case you would substitute in Start_Hour, Start_Minute, Finish_Hour, Finish_Minute.

Substract 2 hours from datetime on Snowflake

I am working on Snowflake, need to substract 2 hours from specifc date:
date time: 2021-06-10 14:07:04.848 -0400
'2021-06-10 14:07:04.848 -0400' - 2 hours
expected result: 2021-06-10 12:07:04.848 -0400 (now it's twelve o'clock).
Datediff didn't work:
DATEDIFF(hour,2,TO_DATE(substr(p.insertedon,1,10)))
There is any simple way to do this? just a subtraction of 2 hours to date time
Regards
Using INTERVAL:
SELECT p.insertedon - INTERVAL '2 HOURS'
FROM ...

How to use datediff function to show day, hour, minute, and second

I have a record:
Depart_dt Arrived_dt
10/1/2013 6:15:00 AM 10/1/2013 7:25:00 AM
Based on my calculation, it is 0 day, one hour, and 10 minutes between the arrived_dt and Depart_dt. How do I show the result like below:
Day Hour Minute Second
0 1 10 0
You can use modulus division on the output of the DATEDIFF() function for this:
SELECT DATEDIFF(second,Depart_dt,Arrived_Dt)/(60*60*24) AS Day
,DATEDIFF(second,Depart_dt,Arrived_Dt)/(60*60)%24 AS Hour
,DATEDIFF(second,Depart_dt,Arrived_Dt)/(60)%60 AS Minute
,DATEDIFF(second,Depart_dt,Arrived_Dt)%60 AS Second
FROM Table1
Demo: SQL Fiddle
select
datediff(second,depart_dt, arrived_dt)/86400 as Day,
datediff(second,depart_dt, arrived_dt)/3600%24 as Hour,
datediff(second,depart_dt, arrived_dt)/60%60 as Minute,
datediff(second,depart_dt, arrived_dt)%60 as Second
from yourtable
Converting days, hours and minutes into seconds for accuracy.
SQL Fiddle: http://sqlfiddle.com/#!6/c6c63/1

Check and calulate time period between datetime in sql 2005

I need to check if any work time period is between datetime and calculate hours between for work hours sum grater than 8 hours.
Sample data:
1. Work time: 07:00 - 17:00 and datetime for checking 06:00-22:00
Answer: 2 hour
2. Work time: 13:00 - 23:00 and datetime for checking 06:00-22:00
Answer: 1 hour (only 1 hour is grater 8 hours and between 06:00-22:00 )
3. Work time: 19:00 - 05:00 and datetime for checking 22:00-06:00
Answer: 2 hour (only 2 hour is grater 8 hours and between 22:00-06:00 )
Any ideas?
Try this: T-SQL DateDiff - partition by "full hours ago", rather than "times minutes turned 00 since"
Basically, DATEDIFF(HOUR, endTime, startTime). Then, subtract your result from 8 to get the difference (if they were "under" time, the difference will be negative).