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
Related
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.
I want to sum all the time differences to show the total hours worked.
select
aaaa
from
employee B
inner join
(select
s.emp_reader_id,
Sum(case when s.in_time is not null and s.out_time is not null and s.shift_type_id=5 and LOWER(DATENAME(dw, [att_date]))='friday'then
cast(datediff(minute,'00:00:00', '23:59:59') / 60 +
(datediff(minute,'00:00:00', '23:59:59') % 60 / 100.0) as decimal(7, 4)
) end) as aaaa
from
Daily_attendance_data s
left outer join
employee bb on s.emp_reader_id = bb.emp_reader_id
where
att_date between '2018-10-01' and '2018-10-31'
and s.emp_reader_id = 1039
group by
s.emp_reader_id) A on B.emp_reader_id = A.emp_reader_id
Current output:
aaaa
47.1800
which gives the list of times by hours but then I want to sum it up to a grand total.
It would just total
Sample data :
23:59
23:59
Expected output:
47.58
I think you should convert all times to seconds, calculate the SUM then convert the total to HH:mm:ss.
Calculate The SUM of seconds
DECLARE #TimeinSecond as integer = 0
select #TimeinSecond = Sum(DATEDIFF(SECOND, '0:00:00', [WorkHrs]))
from Daily_attendance_data
Convert the HH:mm:ss Format
SELECT RIGHT('0' + CAST(#TimeinSecond / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST((#TimeinSecond / 60) % 60 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(#TimeinSecond % 60 AS VARCHAR),2)
References
How to convert hh:mm:ss to seconds in SQL Server with more than 24 hours
SQL SERVER – Convert Seconds to Hour : Minute : Seconds Format
If your date type is DateTime.
you can try to let your value split two part.
hours get value need to condider carry from minutes so do SUM(intpart) + SUM(floatpart) / 60
minutes get value from SUM(floatpart) % 60
look like this.
SELECT concat(SUM(intpart) + SUM(floatpart) / 60,':', SUM(floatpart) % 60)
FROM (
SELECT cast(SUBSTRING (cast(col as varchar),0,3) as int) intpart,
cast(SUBSTRING (cast(col as varchar),CHARINDEX(':',col) +1,2)as int) floatpart
FROM T
) t1
sqlfiddle
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.
I need to convert minutes into hours in sql server.
I used following logic to do it.
CAST(REPLACE(LEFT(CONVERT(varchar(10), DATEADD(MINUTE, 19.80 *100, ''), 114),5),':','.') AS Decimal(5,2)) AS tpschedhours
My expected Output is 33 hours (1980 minutes in hours)
But I got output as 9 hours. I have found that, the issue occurs because DATEADD(MINUTE,1980, '') returns ouptut as 1900-01-02 09:00:00.000 (One day + 9 hours). But I need the Output as Hours value i.e 33 hours
Thanks for the help
SELECT CONVERT(varchar(5),
DATEADD(minute, DATEDIFF(minute, #StartDate, #EndDate), 0), 114)
You can try in following:
DECLARE #time INT = 1980
SELECT LEFT(CONVERT(VARCHAR(10), DATEADD(MINUTE, #time / 60 + (#time % 60), ''),114),5)
I Got the solution from the answers.
SELECT CAST((CAST(((2.72) *100)AS INT) / 60 )+ (CAST((2.72 *100)AS INT) % 60) / 100.0 AS DECIMAL(5,2)).
thanks tinka and Stanislovas Kalašnikovas