I have a column Time in my table. It holds a time value in minutes, ie: 605. What I want it to show it as 10:05, as hh:mm format.
I am doing it like this:
...
Time = cast(AVG(Time) / 60 as varchar) + ':' + cast(AVG(Time) % 60 as varchar),
...
It shows the value as 10:5, but I want to make it as 10:05 and I couldn't find out a way to add '0' before '5' in the minutes section.
Any help would be appreciated.
Try this:
SELECT
TIME = CASE WHEN #t < 600 THEN '0' ELSE '' END +
CAST(Time / 60 as varchar(10)) + ':' +RIGHT(100 +(Time % 60), 2)
Example:
DECLARE #t int = 55121
SELECT
TIME = CASE WHEN #t < 600 THEN '0' ELSE '' END +
CAST(#t / 60 as varchar(10)) + ':' +RIGHT(100 +(#t % 60), 2)
Result:
Time
918:41
You could simply add a Right('0' + ...,2) expression as below to get the '0' in there.
Time = cast(AVG(Time) / 60 as varchar) + ':' + RIGHT('0'+cast(AVG(Time) % 60 as varchar),2)
To answer your question in comments, getting DD:HH:MM is a similar setup.
DayTime = cast(AVG(Time) / 1440 as varchar) + ':' + RIGHT('0' + cast((AVG(Time) / 60) % 24 as varchar),2) + ':' + RIGHT('0'+cast(AVG(Time) % 60 as varchar),2)
You can use a case expression to do this.
Also you should always specify the length of varchar when you use cast, or it would only show the first character.
case when len(cast(AVG(Time)%60 as varchar(2))) < 2 then '0'+cast(AVG(Time)%60 as varchar(2))
else cast(AVG(Time)%60 as varchar(2))
end
Related
I have a Query that shows downtime in hours, minutes but would need it to show it in Days,hours,minutes and maybe Another version that shows Days,hours
I'm not good at coding, sorry.
${SQL: SELECT CAST( CAST(('${N=Alerting;M=Downtime}') AS int) / 60 AS varchar) + ' hours ' + right('0' + CAST(CAST(('${N=Alerting;M=Downtime}') AS int) % 60 AS varchar(2)),2) + ' minutes'}
${SQL: SELECT CAST( CAST(('${N=Alerting;M=Downtime}') AS int) / 1440 AS varchar) + ' days ' + right('0' + CAST(CAST(('${N=Alerting;M=Downtime}') AS int) % 1440 / 60 AS varchar(2)),2) + ' hours ' + right('0' + CAST(CAST(('${N=Alerting;M=Downtime}') AS int) % 60 AS varchar(2)),2) + ' minutes'}
I can't seem to find out how to get the total number of hours from varchar column from SQL Server 2016.
Query:
SELECT taakuren
FROM taken
Returns
10:00
12:15
26:00
40:00
I would like it to return
88:15
I've tried things like below query but a always end up with issue when it gets past 24 hours
select Convert(Varchar, (Convert(DateTime, taakuren)), 114) as TotalTime
from taken
Basically, the time data type peters out at 24 hours. If you want more hours, then you may have to resort to your own arithmetic:
select cast(sum(hours * 60 + minutes) / 60 as varchar(10)) + ':' +
right('00' + cast(sum(hours * 60 + minutes) % 60 as varchar(10)))
from taken t outer apply
(values (left(t.taakuren, 2) + 0, right(t.taakuren, 2) + 0)
) v(hours, minutes);
update my code is:
select cast(sum(hours * 60 + minutes) / 60 as varchar(10)) + ':' +
cast(sum(hours * 60 + minutes) % 60 as varchar(10))
from taken t outer apply
(values (left(t.taakuren, 2) + 0, SUBSTRING(t.taakuren, 4,6) + 0)
) v(hours, minutes)
I am trying to convert a field with positive and negative seconds to HH:MM:SS but failing in the process.
CONVERT(VARCHAR(6), RPAYCODE.TIMEINSECONDS/3600) +
':' +
RIGHT('0' + CONVERT(VARCHAR(2), (RPAYCODE.TIMEINSECONDS % 3600) / 60), 2)
AS "Pay Code Hrs"
When the seconds are positive it works fine = 00:30:00
When the seconds are negative I get this = 0:0*
This should handle the negative values:
CASE
WHEN #time_in_seconds < 0 THEN '-'
ELSE ''
END +
CONVERT(VARCHAR(6), ABS(RPAYCODE.TIMEINSECONDS)/3600) + ':' +
RIGHT('0' + CONVERT(VARCHAR(2), (ABS(RPAYCODE.TIMEINSECONDS) % 3600)/60), 2) AS [Pay Code Hrs]
Lean and simple conversion
Skip all those unnecessary calculations and string operations:
CASE WHEN RPAYCODE.TIMEINSECONDS < 0 THEN '-' ELSE '' END -- sign
+ CONVERT(varchar, DATEADD(s, ABS(RPAYCODE.TIMEINSECONDS), 0), 108) -- hh:mm:ss
Number format 108 is hh:mm:ss.
I have a function like this:
ALTER FUNCTION [dbo].[testfunctionstacknew] (#dec NUMERIC(18, 2)) RETURNS Varchar(50)
AS
BEGIN
DECLARE
#hour decimal(18,2),
#Mns decimal(18,2),
#second decimal(18,3)
DECLARE #Average Varchar(50)
select #hour=CONVERT(int,#dec/60/60)
SELECT #Mns = convert(int, (#dec / 60) - (#hour * 60 ));
select #second=#dec % 60;
SELECT #Average =
convert(varchar(9), convert(int, #hour)) + ':' +
-- right('00' + convert(decimal(10,0), convert(decimal(18,2), #hour)), 2) + ':' +
right('00' + convert(decimal(10,0), convert(decimal(18,2), #Mns)), 2) + ':' +
right('00' + CONVERT(decimal(10,0), convert(varchar(10), #second)), 6)
RETURN #Average
END
and i have a stored procedure like this:
select dbo.testfunctionstacknew(
convert(decimal(10,1),
avg(convert(numeric(18,2), datediff(ss, t.dtime, t.PAICdate ))))
) as Avgparkingtime from (select top 10 * from transaction_tbl where locid=6 and dtime >= getdate()-1 order by transactID desc ) t
while executing stored procedure if the average time is less than 60 minutes i am getting result like this:
Avgparkingtime :
0:25:33
if the average time is less than 60 minutes then i dont want to get zero in front of minutes,,(this time only need to show minutes and seconds)..only hour need to show if the minutes is greater than 60 minutes...how i can do this? what changes i have to make in my function ?? any help is very appreciable..
Try changing the part where you build the formatted string like this:
SELECT #Average =
case
when convert(int, #hour) <> 0 then convert(varchar(9), convert(int, #hour)) + ':'
else ''
end +
right('00' + convert(decimal(10,0), convert(decimal(18,2), #Mns)), 2) + ':' +
right('00' + CONVERT(decimal(10,0), convert(varchar(10), #second)), 6)
Is there a easy way to format a float number in hours in Ms SQL server 2008?
Examples:
1.5 -> 01:30
9.8 -> 09:48
35.25 -> 35:15
Thanks a lot.
I like this question!
DECLARE #input float = 1.5;
DECLARE #hour int = FLOOR(#input);
DECLARE #minutes int = (SELECT (#input - FLOOR(#input)) * 60);
SELECT RIGHT('00' + CONVERT(varchar(2), #hour), 2) + ':' + RIGHT('00' + CONVERT(varchar(2), #minutes), 2);
SELECT SUBSTRING(CONVERT(NVARCHAR, DATEADD(MINUTE, 1.5*60, ''), 108), 1, 5)
This works by:
starting from the "zero" date
adding 1.5 x 60 minutes (i.e. 1.5 hours)
formatting the result as a time, hh:mm:ss (i.e. format "108")
trimming off the seconds part
It is necessary to use 1.5 x 60 minutes instead of 1.5 hours as the DATEADD function truncates the offset to the nearest integer. If you want high-resolution offsets, you can use SECOND instead, suitable scaled (e.g. hours * 60 * 60).
Sure. Easy, but not exactly...straightforward:
declare #hours float
set #hours = -9.8
select substring('- ',2+convert(int,sign(#hours)),1) -- sign
+ right('00' + convert(varchar, floor(abs(#hours))) , 2 ) -- hours component
+ ':' -- delimiter
+ right('00' + convert(varchar,round( 60*(abs(#hours)-floor(abs(#hours))) , 0 ) ) , 2 ) -- minutes
Another option that will give the correct result. You might need to tweak it to round minutes and to ensure that both fields are 2 digits wide.
declare #hours float
set #hours = -9.8
select convert(varchar, datediff(minute,dateadd(minute,#hours*60,convert(datetime,'')),'') / 60 )
+ ':' + convert(varchar, datediff(minute,dateadd(minute,#hours*60,convert(datetime,'')),'') % 60 )
WITH m AS
SELECT Minutes = CAST(#hours * 60 AS int)
)
SELECT CAST(Minutes / 60 AS varchar) + ':' + RIGHT(100 + Minutes % 60, 2)
FROM m
select dateadd(MINUTE, cast((8.18 % 1) * 60 as int), dateadd(hour, cast(8.18 as int), convert(varchar(10), getdate(), 10)))