how to convert hours to days - sql

I have an hours value that I would like to convert to days, such as 1.97 hours to days or 26.50 hours to days. How do I do this?

The easiest way would be to do a simple and standard unit conversion - just divide by 24
Select 1.94 as hour, 1.94/24 as days

Related

Date_diff with specific condition time start and time end

is it possible to have date_diff with specific start and end time?
let say my store are open from 8AM - 10PM, which is 14 Hours.
and I have a lot of stuff to sell during that time. One of the SKU is out of stock from 2022-11-01 06.00 PM until tomorrow 2022-11-02 11.00 AM.
Instead of calculate 24 hours, I just want to calculate only from opening store until it closed or until its restock. Meaning from 6PM to 11AM is 8 Hours
my query
select date_diff('2022-11-02 11.00 AM', '2022-11-02 06.00 PM', hour) from table
with the result 17 hours instead of 8 hours
There isn't a way to configure DATE_DIFF to do this for you, but it's possible to do what you want, with some effort.
You should convert your dates to timestamps (TIMESTAMP(yourdate) or CAST(yourdate AS TIMESTAMP)) and use TIMESTAMP_DIFF instead.
This will allow you to work with smaller intervals than days.
For your calculation, you ultimately need to find the total time difference between the two timestamps and then subtract the out-of-hours timeframe.
However, calculating the latter is not as simple as taking the difference in days and multiplying by 8 hours (10pm-6am), because your out-of-hours calculation has to account for weekends and possibly holidays etc. Hence it can get quite complex, which is where the solution in my first link might come in.

Converting a number to duration ( days, hours minutes and seconds)

Do you know how to convert this duration number 145351401 to days, hours, minutes and seconds in SQL?
I tried
SELECT CAST(STUFF(STUFF(STUFF(cast(145351401 as varchar),3,0,':'),6,0,':'),9,0,'.') AS TIME)
but it gave me the hours, minutes and seconds but no days.
The above number is the difference between the below two dates, so it should have something like 7 days and the hours, minutes and seconds in the duration.
2022-09-08 16:21:48.400
2022-09-16 07:22:31.400

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

Is there any way to get period from sql

I have 2 columns i need to get the different period with days and hours
Example
2019-10-22 13:22:59 min getdate()
I need result to be
3 days 7 hours

Subtracting two dates (including hours and minutes) considering only working time vba or Excel

I need to subtract two dates (including hours and minutes), but I only need to consider working hours. That is, I need to omit lunch time (from 13 to 14 hrs), weekends and hours after 18 hrs and before 9 hrs of the following day, in a working day (from Mo to Fr). Any thoughts?
I don't mind if it's an Excel formula or a vba code.
I have this formula, but it doesn't omit lunch time:
9*(NETWORKDAYS(initial_time;ending_time)-1)-24*((MOD(initial_time;1)-MOD(ending_‌​time;1)))
Here's a possible solution. It assumes an 8 hour work days for all but the start and end date. Also that start date/time is 9:00 or after and end date/time is 18:00 or earlier and that both are on a weekday.
=(NETWORKDAYS(A2,B2)-2)*8+IF(MOD(A2,1)>0.58333,(TIME(18,0,0)-MOD(A2,1))*24,(TIME(18,0,0)-MOD(A2,1))*24-1)+IF(MOD(B2,1)>0.58333,(MOD(B2,1)-TIME(9,0,0))*24-1,(MOD(B2,1)-TIME(9,0,0))*24)
.58333 equates to 14:00. The formula:
multiplies networkdays * 8
+ hours from start date/time until 18:00 subtracting 1 hour if start time is before 14:00
+ hours from 9:00 until end date/time subtracting 1 hour if end time is after 14:00
Of course this doesn't take any holidays into account.