I've got some code that generates hours between an oldest record in the table (MIN) and getdate(), and am using
CONVERT(CHAR(8),DATEADD(SECOND,DATEDIFF(SECOND,MIN(OLDESTRECORD),GETDATE()),0),108)
to get the HH:MM:SS, but when the hours is more than 24 hours it just shows the hours part. i.e. 30 hours difference shows as 6 hours... how do I get it to show dd.hh:mm:ss i.e. 1.06:00:00
watch out for the case where the 'time' in the start day has not been reached in the end date
select cast(DATEDIFF(SECOND,MIN(OLDESTRECORD),GETDATE()) / 86400 as varchar(4)) + '.' + CONVERT(CHAR(8),DATEADD(SECOND,DATEDIFF(SECOND,MIN(OLDESTRECORD),GETDATE()) % 86400 ,0),108)
Related
I am trying to add seconds to an existing date to find the end time. I have start time in one field and total duration in one field , i find in internet that we can add number to date but we should to dived on 86400
(chargestarttime + (NVL (callduration, 0) / 86400))
// where chargestarttime is date , callduration is number
// sample (chargestarttime is 2020-05-30 02:21:58 and callduration is 65 the output is 2020-05-30 02:23:03)
my question is why we need to div on 86400 ? if I convert the date to UNIX_TIMESTAMP and then add just the 65 it will give the same result without div on 86400 (24 hours) or there are a reason that we div on 86400 (24 hours)
why we need to div on 86400?
In date arithmetics, Oracle uses 1 to represent 1 day. If you start from a number of seconds, you need to convert that to days, so
chargestarttime + NVL(callduration, 0) / 60 / 60 / 24
-- seconds per minutes-----^
-- minutes per hour -----^
-- hours per day -----^
86400 is just the number of seconds there is in a day.
Your query is adding seconds as a fraction of a whole day (i.e. 1 day = 24 hours = 1440 minutes = 86400 seconds).
Alternatively, you could add seconds directly to the date using an INTERVAL data type:
chargestarttime + NVL(callduration, 0) * INTERVAL '1' SECOND
or
chargestarttime + NUMTODSINTERVAL( NVL(callduration, 0), 'SECOND' )
I am creating a query that shows me the time elapsed between two dates, only taking into account only the one that is Monday through Friday from 08:00 to 17:00, for example:
For example, if a petition opens on day 1 at 6:30 p.m. and closes on day 2 at 8:45 p.m., the TMO is 45 minutes.
If it closes on day 3 at 8:45, the TMO is 9 hours and 45 minutes.
Example 2:
If a petition opens on Friday at 16:45 and closes on Tuesday at 8:30, the MTO would be: 15 minutes on Friday, nine hours on Monday and 30 minutes on Tuesday for an MTO = 9 hours 45 minutes
The query is performed on a single column of type date as I show below
I currently use a LAG function to make the query, but I can not create something functional, not even optimal to incorporate, I would greatly appreciate your help.
In the solution below I will ignore the "lag" part of your problem, which you said you know how to use. I am only showing how to count "working hours" between any two date_times (they may be during or before or after work hours, and/or they can be on weekend days; the computation is the same in all cases).
Explaining the answer in words: For two given date-times, "start" and "end", calculate how many "work" hours elapsed from the beginning of the week (from Monday 00:00:00) till each of them. This is in fact a calculation for ONE date, not for TWO dates. Then: given "start" and "end", calculate this number of hours for each of them; subtract the "end" number of hours from the "start" number of hours. To the result, add x times 5 times 9, where x is the difference in weeks between Monday 00:00:00 of the two dates. (If they are in the same week, the difference will be 0.)
To truncate a date to the beginning of the day, we use TRUNC(dt). To truncate to the beginning of Monday, TRUNC(dt, 'iw').
To compute how many "work" hours are from the beginning of the date dt until the actual time-of-day we can use the calculation
greatest(0, least(17/24, dt - trunc(dt)) - 8/24)
(the results will be in days; we calculate everything in days and then we can convert to hours). However, in the final formula we must check to see if the date is a Saturday or Sunday, in which case this should just be zero. Or, better, we can adjust the calculation a bit later, when we count from the beginning of Monday (we can use least( 5*9/24, ...)).
Putting everything together:
with
inputs ( dt1, dt2 ) as (
select to_date('2017-09-25 11:30:00', 'yyyy-mm-dd hh24:mi:ss'),
to_date('2017-10-01 22:45:00', 'yyyy-mm-dd hh24:mi:ss')
from dual
)
-- End of SIMULATED input dates (for testing only).
select 24 *
( least(5 * (17 - 8) / 24, greatest(0, least(17/24, dt2 - trunc(dt2)) - 8/24)
+ (17 - 8) / 24 * (trunc(dt2) - trunc(dt2, 'iw')))
-
least(5 * (17 - 8) / 24, greatest(0, least(17/24, dt1 - trunc(dt1)) - 8/24)
+ (17 - 8) / 24 * (trunc(dt1) - trunc(dt1, 'iw')))
+ 5 * (17 - 8) / 24 * (trunc(dt2, 'iw') - trunc(dt1, 'iw')) / 7
)
as duration_in_hours
from inputs
;
DURATION_IN_HOURS
-----------------
41.500
Lets say I have a table which storing HOURS and MINUTES in two different column.
For example:
HOURS|MINUTES
------------------
1|11
2|22
3|33
How can I merge the hours and minutes in my table into current timestamp
My concept are as below
SELECT TRUNC(CURRENT TIMESTAMP) + HOURS + MINUTES FROM MYTABLE;
try this one
select current timestamp + HOURS hour + MINUTES minute
from sysibm.sysdummy1
After several hours of trying i find out that i need to further enhance my table design, instead of storing minute and hours in different column, is better that i store in in single column. The sample value can be '18:36:00'
then use the following query to convert it
(SELECT CAST(? AS TIMESTAMP) + HOUR(TIME(END_TIMESTAMP)) HOURS + MINUTE(TIME(END_TIMESTAMP)) MINUTES FROM MY_TABLE
I use the following code in my statement.
datediff(minute, visit.nexttime, visit.endtime)
the results varies.
I will show u a couple of results.
0:24:26 - 25
0:32:14 - 32
0:05:12 - 5
0:06:28 - 7
0:03:52 - 4
0:03:32 - 3
first one, 24 rounds up to 25 while there is only 26 seconds.
second one, 32 remains where the second is 14.
third one, 5 remains 5, fair enough.
fourth one, 6 rounds up to 7 while there are only 28 seconds.
fifth one, 3 round up to 4 coz of 52 seconds.
last one, 3 remains 3 even there r 32 seconds.
Why is that?
I use the following code to get my first column.
CONVERT(varchar(6), datediff(second, visit.nexttime, visit.endtime)/3600)
+ ':' +
RIGHT('0' + CONVERT(varchar(2), (datediff(second, visit.nexttime, visit.endtime) % 3600) / 60), 2)
+ ':' +
RIGHT('0' + CONVERT(varchar(2), datediff(second, visit.nexttime, visit.endtime) % 60), 2)
as 'Transaction Time'
I'm not actually familiar with SQL Server, however reading the documentation here i suspect i know why you are getting those results.
The return value is described as this:
Returns the count (signed integer) of
the specified datepart boundaries
crossed between the specified
startdate and enddate.
Since you specified minute for the datepart, I would expect this means the seconds are ignored and it just counts how many times the minute changes between the startdate and enddate.
eg.
If the startdate and enddate were 50 seconds apart in the same minute, it would return 0.
If the startdate and enddate were only 5 seconds apart but in different minutes, it would return 1.
Datediff is a bit strange and rather nonintuitive. And it works this way since old Sybase days.
In your case datediff(m, x, y) completly ignores the seconds.
You have the same problem when you try to calculate ages:
select datediff (y, '2010-12-31', '2011-01-01')
does't mean that the rounded difference is about 1 year, it only says that 1 new year happend.
To get the results you intend, you must take the difference at a more granular base, as you did using seconds.
Please refer to the MySql documentation. It should clarify this for you.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
Consider two dates 2010-03-18 22:30:45 and 2010-03-19 03:30:15 .... How to get the number of hours and minutes in between the two dates in sql server.....
#codeka answered with the hours part (from your title) but in the body of your question you asked for hours and minutes so, here is one way
select DATEDIFF(hh, #date1, #date2) as Hours_Difference,
DATEDIFF(mi,DATEADD(hh,DATEDIFF(hh, #date1, #date2),#date1),#date2) as Minutes_Difference
What this does in the first part is what #codeka showed. It gives you the datediff between the two dates in actual full hours. The second term in the sql gives the datediff in minutes between the (first date + the hours elapsed) and the second date. You have to eliminate the hours from the equation in the minutes part or you will get the actual minutes between the dates. Datediff and its allowed Datepart identifiers can be researched here:
http://msdn.microsoft.com/en-us/library/ms189794.aspx
You want the DATEDIFF function:
SELECT DATEDIFF(hh, #date1, #date2)
The problem with William Salzman's answer is that it returns strange answers if the first time is not on the hour. So 10:30 to 12:00 gives 2 hours and -30 minutes.
If that isn't what you want, then this will give you 1 hour and 30 minutes:
select
CONVERT(int,DATEDIFF(mi, #date1, #date2) / 60) as Hrs_Difference,
CONVERT(int,DATEDIFF(mi, #date1, #date2) % 60) as Mins_Difference
DATEDIFF function will solve your problem of getting hours difference.
you can check this as :
select DATEDIFF(hh, getdate()-1, GETDATE()) HoursDifference
It will give you result of 24 hours
Thanks
It seems there are various ways to answer this, but for me, I've always believe in the notion of starting small - as in get the overall value and then convert it UP to the answer you want.
Assuming date2 is greater than date1:
A simple mathematical difference multiplied by 24 hours in a day works:
CAST(date2 - date1 as NUMERIC(18,4)) * 24.00 AS [HrsDiff]
Or using Will's answer using [min] for minutes:
CAST(DATEDIFF(mi, date1, date2) AS NUMERIC(18,4)) / 60 AS [HrsDiff]
So the minute total is divided by 60 to give you the hours.
The first option gives you a big decimal, whereas the 2nd option rounds it up to the nearest decimal.