I need convert a millisecond value, 85605304.3587 to a value like 0d 18h 21m.
No idea on how to start that, is there something similar to a TimeSpan in SQL like there is in C#?
You can do the calculation explicitly. I think it is:
select floor(msvalue / (1000 * 60 * 60 * 24)) as days,
floor(msvalue / (1000 * 60 * 60)) % 24 as hours,
floor(msvalue / (1000 * 60)) % 60 as minutes
Note: Some databases use mod instead of %.
In MS SQL SERVER you can use next code:
with cte as (
select cast(85605304.3587 as int) / 1000 / 60 as [min]
), cte2 as (
select
cast([min] % 60 as varchar(max)) as minutes,
cast(([min] / 60) % 24 as varchar(max)) as hours,
cast([min] / (60 * 24) as varchar(max)) as days
from cte
)
select concat(days, 'd ', hours, 'h ', minutes, 'm') as tm
from cte2
Using native date & time functions, maybe:
SELECT
AsDateTime = DATEADD(MILLISECOND, 85605304, 0)
, AsDateTime2 = DATEADD(NANOSECOND, 7 * 100, DATEADD(MICROSECOND, 358, DATEADD(MILLISECOND, 85605304, CONVERT(datetime2, CONVERT(datetime, 0)))))
-- Incorrect datetime2 approach I initially did, has some precision loss, probably due to datetime's millisecond issue with 0's, 3's, and 7.'s
--SELECT DontDoThis = DATEADD(NANOSECOND, 7 * 100, DATEADD(MICROSECOND, 358, CONVERT(datetime2, DATEADD(MILLISECOND, 85605304, 0))))
datetime covers only 3 digits beyond seconds, while datetime2 will maintain 7 digits. Perhaps other ways that give date-like objects exist, I wouldn't know.
Related
I have this column Break in integer format.
The values are:
100 = 1h
130 = 1h and 30 minutes
I need to extract the minutes from this column:
100 = 1h = 60 minutes
130 = 1h and 30m = 90 minutes
I tried with this SQL, but it doesn't work:
select
convert(int, datediff(minute, 0, convert(varchar(8), dateadd(minute, TSH1."Break", ''), 114) ))
from TSH1
Can anyone help me?
Thanks
The following code will produce the output you want
select
(((CAST(TSH1."Break" AS INT) / 100) * 60) + (CAST(TSH1."Break" AS INT) % 100)) As col
from TSH1
I have a script which rounds a time length to the nearest 15 minutes. So for example 00:23:00 minutes to 00:37:59 duration will return 0.5 (decimal 30 minutes) etc continuous around the clock. I am trying to now get the script to work in half minutes so I need to shift this by 30 second offset for the rounding, so 00:22:29 seconds will go to 0.25 and 00:22:30 will round to 0.5. Here is the original script. So this needs to return 0.5 not 0.25. If you change the #finish to 08:22:29 it needs to then return 0.25. Thank you
declare #start datetime = '2020-03-01 08:00:00:000'
declare #finish datetime = '2020-03-01 08:22:30:000'
select
(CAST(DATEPART(hh, (DATEADD( minute, ( DATEDIFF( minute, 0, DATEADD( second, ( 15 * 60 ) / 2, (convert(varchar(5),(#finish - #start),108)) ) ) / 15 ) * 15, 0 ))) AS float) +
CAST(DATEPART(mi, (DATEADD( minute, ( DATEDIFF( minute, 0, DATEADD( second, ( 15 * 60 ) / 2, (convert(varchar(5),(#finish - #start),108)) ) ) / 15 ) * 15, 0 ))) AS float) / 60 +
CAST(DATEPART(ss, (DATEADD( minute, ( DATEDIFF( minute, 0, DATEADD( second, ( 15 * 60 ) / 2, (convert(varchar(5),(#finish - #start),108)) ) ) / 15 ) * 15, 0 ))) AS float) / 3600)
Here's another possible solution, using CASE to classify the modulo seconds over an hour:
SELECT DATEDIFF(second,#start,#finish) / 3600 +
CASE WHEN DATEDIFF(second,#start,#finish) % 3600 < 450 THEN 0.0
WHEN DATEDIFF(second,#start,#finish) % 3600 < 1350 THEN 0.25
WHEN DATEDIFF(second,#start,#finish) % 3600 < 2250 THEN 0.5
WHEN DATEDIFF(second,#start,#finish) % 3600 < 3150 THEN 0.75
ELSE 1
END
Solution is to add a dateadd(second, 30, #finish) I believe. Not the start time, this will then either push the result to over 00:23 if 00:22:30 and above or keep the length below 00:23. This should do the trick
In your solution, the problem is starting from the division of two integer numbers that would result to an integer. You can check for a solution.
Since you are dealing with time, it might be appropriate to use the CEILING function. If you think that this way is more appropriate, you could use:
declare #start datetime = '2020-03-01 08:00:00:000'
declare #finish datetime = '2020-03-01 08:22:30:000'
SELECT CEILING( DATEDIFF( ss, #start, #finish)/ (60*60.00) * 4) / 4
I have the following code which is working, however how do I get the terminal minutes to show the output as Day, Hour, Minute? If this cannot be done, is it possible to add a +1 on the time which would indicate it's the following day?
The problem I am having is that when our orders run past 23.59 PM, the system is not displaying the correct format because of the 24 hour time period.
I am stumped and hope I am not confusing matters.
SELECT FOLIO_NUMBER, TERMINAL_NAME,
format((START_LOAD_TIME - ORDER_ENTRY_TIME), 'HH:mm') AS STAGING_MINUTES,
format((TERM_END_LOAD_TIME - START_LOAD_TIME), 'HH:mm') AS LOADING_MINUTES,
format((TERM_END_LOAD_TIME - ORDER_ENTRY_TIME), 'HH:mm') AS TERMINAL_MINUTES
FROM ORDERS
JOIN TERMINAL_OWNER ON ORDERS.LOADING_TERMINAL_ID = TERMINAL_OWNER.TERMINAL_ID
DECLARE #INT INT
SET #INT = DATEDIFF(SECOND,GETDATE(),GETDATE()+1)
select
convert(varchar(10), (#INT/86400)) + ':' +
convert(varchar(10), ((#INT%86400)/3600)) + ':'+
convert(varchar(10), (((#INT%86400)%3600)/60)) + ':'+
convert(varchar(10), (((#INT%86400)%3600)%60)) as 'DD:HH:MM:SS'
Courtesy of Nat-MS. See here
I think you are looking for something like this
Declare #theMinutes Varchar(10)
Set #theMinutes = '19:25'
declare #totMintute int
Select
#totMintute = (Cast(
Cast(left(#theMinutes,charindex(':',#theMinutes)-1) as Int) * 60
+ Cast(substring(#theMinutes,charindex(',',#theMinutes)+4,len(#theMinutes)) as Int)
as Int ) * 60) / 60
--For 12 hour 1 days
Select #totMintute / 720 as NoDays -- 720 minutes per day
, (#totMintute % 720) / 60 as NoHours -- modulo 720
, (#totMintute % 60) as NoMinutes -- modulo 60
--For 24 hour 1 days
Select #totMintute / 1440 as NoDays -- 1440 minutes per day
, (#totMintute % 1440) / 60 as NoHours -- modulo 1440
, (#totMintute % 60) as NoMinutes -- modulo 60
The output will look like as shown below.
You can convert this query data source table as shown below.
Create table #Temp (MinValue Varchar(8))
insert into #Temp Values ('19:25')
Select TotMinute / 720 as NoDays -- 1440 minutes per day
, (TotMinute % 720) / 60 as NoHours -- modulo 1440
, (TotMinute % 60) as NoMinutes -- modulo 60
from(
select
(Cast(
Cast(left(MinValue,charindex(':',MinValue)-1) as Int) * 60
+ Cast(substring(MinValue,charindex(',',MinValue)+4,len(MinValue)) as Int)
as Int ) * 60) / 60 as TotMinute
from #Temp
)a
You can find the live demo here.
I would say that you should remove the need to identify days/hours in the SQL Output and just get the difference in minutes, which you can then work with in your application layer.
Take this sample code:
create table #orders (
FOLIO_NUMBER int,
START_LOAD_TIME datetime,
ORDER_ENTRY_TIME datetime,
TERM_END_LOAD_TIME datetime
)
insert into #orders (FOLIO_NUMBER,START_LOAD_TIME,ORDER_ENTRY_TIME,TERM_END_LOAD_TIME)
values (1, getdate(),getdate() - 1,getdate() + 1)
select *,
datediff(mi, ORDER_ENTRY_TIME, START_LOAD_TIME) AS STAGING_MINUTES,
datediff(mi, START_LOAD_TIME, TERM_END_LOAD_TIME) AS LOADING_MINUTES,
datediff(mi, ORDER_ENTRY_TIME, TERM_END_LOAD_TIME) AS TERMINAL_MINUTES
from #orders
drop table #orders
This will output the minutes difference between the events:
FOLIO_NUMBER STAGING_MINUTES LOADING_MINUTES TERMINAL_MINUTES
1 1440 1440 2880
You can then perform some simple maths with these values to extract, days, hours and minutes.
My output is like this
**Column1** **Column2**
20170123012057.555 20170123070616.314
I should get 20719 seconds if I minus the two columns
You can convert the values to a date and then take the difference:
select (to_date(substr(column2, 1, 14), 'YYYYMMDDHH24MISS') -
to_date(substr(column1, 1, 14), 'YYYYMMDDHH24MISS')
) * 24 * 60 * 60
Try this. Here I have given difference in milliseconds as well as in seconds which is rounded
select
extract(day from (to_timestamp(column2,'YYYYMMDDHH24MISS.FF')
-to_timestamp(column1,'YYYYMMDDHH24MISS.FF')
)*86400*1000) / 1000
as diff_in_milliseconds
,round(extract(day from (to_timestamp(column2,'YYYYMMDDHH24MISS.FF')-
to_timestamp(column1,'YYYYMMDDHH24MISS.FF')
)*86400*1000) / 1000
) as diff_in_seconds
from
(select
'20170123012057.555' as Column1,
'20170123070616.314' as Column2
from dual)
Output
DIFF_IN_MILLISECONDS DIFF_IN_SECONDS
20718,7590 20719
Try as
SELECT ROUND (seconds / 1000) seconds
FROM (SELECT EXTRACT (DAY FROM cal_col) * 24 * 60 * 60 * 1000
+ EXTRACT (HOUR FROM cal_col) * 60 * 60 * 1000
+ EXTRACT (MINUTE FROM cal_col) * 60 * 1000
+ ROUND (EXTRACT (SECOND FROM cal_col) * 1000)
seconds
FROM (SELECT TO_TIMESTAMP ('20170123070616.314',
'YYYYMMDDHH24MISS.FF')
- TO_TIMESTAMP ('20170123012057.555',
'YYYYMMDDHH24MISS.FF')
cal_col
FROM DUAL))
I have multiple records as workinghours of employee as
02:10:00
03:00:00
12:00:00
12:34:56
Now i need to add these record and want to display in
`hh:mm:ss`
Format. I used following query but it only works when sum of record less than 24 hours but record may be greater than 24 hours.
SELECT CAST(DATEADD(MILLISECOND,
SUM(DATEDIFF(MILLISECOND, '00:00:00.000'
, CAST(WorkHrs AS TIME))), '00:00:00.000') AS TIME) AS Total_Time
FROM tblAttend
I spend more than 4 hours on google to find solution but got no success anymore.
Datatype of workinghour column is varchar .
The time data type has an explicit range only up to 24 hours (see here).
So, I think you are basically stuck with doing the conversion yourself. It is ugly, but looks something like:
SELECT RIGHT('00' + CAST(SUM(DATEDIFF(MILLISECOND, '00:00:00.000', CAST(WorkHrs AS TIME))), '00:00:00.000')/(60*60) AS VARCHAR(255)), 2)
) +
RIGHT('00' + CAST((SUM(DATEDIFF(MILLISECOND, '00:00:00.000', CAST(WorkHrs AS TIME))), '00:00:00.000')/60 % 60) AS VARCHAR(255)), 2) +
RIGHT('00' + CAST(SUM(DATEDIFF(MILLISECOND, '00:00:00.000', CAST(WorkHrs AS TIME))), '00:00:00.000') % 60 AS VARCHAR(255)), 2)
)
Merhaba Waqas,
Could you please try following SQL CTE Select statement,
It returns hours value more than 24, for this case it was 29 I guess.
Please check time calculation on SQL Server, this select is adapted from there
;with cte as (
select
userid,
total = sum( DATEPART(ss,period) + 60 * DATEPART(mi,period) + 3600 * DATEPART(hh,period) )
from WorkingHours
group by userid
)
select
userid,
total [Total Time in Seconds],
(total / 3600) [Total Time Hour Part],
((total % 3600) / 60) [Total Time Minute Part],
(total % 60) [Total Time Second Part]
from cte
I hope it helps you