Convert minutes in Numeric format to Hours and minutes format - sql

I Have Minutes as 1064 in a column called 'Efforts_in_minutes' and I need the Output in the format of HH:MM (i.e) 17:44. I have tried the below Query
SELECT Cast(Round(Total_Effort_in_Minutes / 60, 0, 2) AS VARCHAR)
+ ':'
+ Cast(Total_Effort_in_Minutes % 60 AS VARCHAR(2))
FROM PPS
I got the output as 17.000000:44 but What i Need is 17:44
Please advice how to achieve that.

The use of the Round function is unnecessary. Just do integer division:
select CAST(CAST(Total_Effort_in_Minutes AS INT) / 60 AS VARCHAR)
+ ':' + CAST(Total_Effort_in_Minutes % 60 AS VARCHAR(2) )
If your column Total_Effort_in_Minutes already has an integer data type, then you can simplify to:
select CAST(Total_Effort_in_Minutes / 60 AS VARCHAR)
+ ':' + CAST(Total_Effort_in_Minutes % 60 AS VARCHAR(2) )
If you need the minute part to be left-padded with zero to get at least 2 digits, then:
select RIGHT('0' + CAST(CAST(Total_Effort_in_Minutes AS INT) / 60 AS VARCHAR), 2)
+ ':' + CAST(Total_Effort_in_Minutes % 60 AS VARCHAR(2) )

SELECT DATEADD(MI,Total_Effort_in_Minutes,TIMEFROMPARTS(0,0,0,0,0))
FROM PPS
Best practice: The client should decide how to represent the time based on the locale, to allow for variants such as 24H clock vs AM/PM.

Please see this link: https://stackoverflow.com/a/19887500/6298495
Creating an inline function is a good solution provided in the link.

Just for fun:
SELECT CONVERT(CHAR(5), DATEADD(mi, 1064, 0), 8)

Related

How to add leading 0's to integer in sql? [duplicate]

This question already has answers here:
Most efficient method for adding leading 0's to an int in sql
(5 answers)
Closed 7 months ago.
I have date and time values that are stored (separately) as integers.
I now want to convert them into datetime data type into a new column but have trouble handling the time.
My first idea was to do it kinda like this (I can deal with the dates so in this example I'm just using a dummy date):
SET new_column = CAST(CONCAT('2020-01-01T',
date_column / 10000, ':',
date_column % 10000 / 100, ':',
date_column % 100) AS DATETIME)
The issue is that CAST expects exactly the format 'yyyy-mm-ddThh:mm:ss' but the calculation may sometimes only return one digit instead of the needed two digits for a part of the time.
For example the time one minute after 9am would result in the string '2020-01-01T9:1:0' though '2020-01-01T09:01:00' would be required.
Is there an efficient way to add leading 0s?
EDIT: The date_column contains integer values that represent times in the format hhmmss. So for example 10am would be stored as 100000, one minute after 9am would be stored as 90100.
I know that it is stupid to encode times/dates as integer. This is for legacy reasons and my current task is to fix it ;)
You need to pad with a zero to deal with single-digit output. Unfortunately, T-SQL doesn't have a PAD or RPAD function, so you have to do it manually:
SET new_column = CONVERT(datetime,
CONCAT('2020-01-01T',
RIGHT(CONCAT('0', date_column / 10000),2)
+ ':'
+ RIGHT(CONCAT('0', date_column % 10000 / 100), 2)
+ ':'
+ RIGHT(CONCAT('0', date_column % 100), 2)));
Example db<>fiddle
Instead of worrying about creating a string to be converted - convert the time to seconds and just add that to your date:
Declare #myDate int = 20220711
, #myTime int = 182233;
Select cast(#myDate As char(10))
, #myTime / 10000 * 60 * 60
, #myTime % 10000 / 100 * 60
, #myTime % 100
, dateadd(second, (#myTime / 10000 * 60 * 60) + (#myTime % 10000 / 100 * 60) + (#myTime % 100), cast(#myDate As char(10)))

SQL Converting to HH:MM:SS with an equation - Not working Correctly

I am trying to minus the sum of one column from the sum of another column in seconds, and then convert it into HH:MM:SS. The conversion however requires it to be able to go over 24 hours as this is a sum time. My conversion works on single columns with no problem so it is clearly the equation i am trying to do it in but cant figure out how to do it.
The 2 columns are Dispo & Dead both are Ints.
Here is what i have:
CONVERT(varchar(6), SUM([Dispo])-SUM(Dead)/3600)
+ ':' + RIGHT('0' + CONVERT(varchar(2), (SUM([Dispo])-SUM(Dead)% 3600) / 60), 2)
+ ':' + RIGHT('0' + CONVERT(varchar(2), SUM([Dispo])-SUM(Dead) % 60), 2) AS [AfterCallWork],
The Result it gives me is:
AfterCallWork
26809:0*:0*
The Result i want for e.g. is:
AfterCallWork
35:51:09
(The number in the e.g. wont necessarily be the result just an example of format)
Sample Data is:
UniqueID | Dead | Dispo
135151 | 20 | 200
161681 | 35 | 421
516168 | 10 | 308
expected results for this sample is 0 hours, 14 minues and 24 seconds:
0:14:24
Dead & Dispo are seconds in a given status or mode. Unique ID is essentially a call identifier.
Here is one way to do it -
Create and populate sample table (Please save us this step in your future questions)
DECLARE #T AS TABLE
(
UniqueID int,
Dead int,
Dispo int
)
INSERT INTO #T VALUES
(135151, 20, 200),
(161681, 35, 421),
(516168, 10, 308)
Use a CTE to get the SUM([Dispo])-SUM(Dead) - This step is only to make the code more readable, you don't need to do it, but it helps:
;WITH CTE(NumberOfSeconds) AS
(
SELECT SUM([Dispo])-SUM(Dead)
FROM #T
)
Convert the number of seconds you got from the CTE to a string representation of hh:mm:ss
SELECT CAST(NumberOfSeconds /3600 as varchar(6)) + ':' +
RIGHT('0' + CAST((NumberOfSeconds % 3600) / 60 as varchar(2)), 2) + ':' +
RIGHT('0' + CAST(NumberOfSeconds % 60 as varchar(2)), 2) As AfterCallWork
FROM CTE
Results:
AfterCallWork
0:14:24
See a live demo on rextester.

Convert seconds to '00:00:00' format - SQL Server

I have a report in which i have to calculate overall worked hours by any employee in a week. The report reflects hours more than 24 hours(or 86400 seconds). I want to convert all the seconds into "00:00:00" format keeping the format of the columns as datetime or time so that I can do further analysis using the time field.
Below is what I have tried so far:
Declare #WorkTimeInSeconds int
Set #WorkTimeInSeconds = 144000 **--This value is an example. Mostly values are above 86400 seconds**
Select ActualHoursWorked = convert(varchar,(#WorkTimeInSeconds)/3600)
+ ':' + right( '0' + convert(varchar,((#WorkTimeInSeconds) %3600)/60),2)
+ ':' + right( '0' + convert(varchar,(#WorkTimeInSeconds) %60),2)
Above code gives me what I need however the above converts the seconds in INT to varchar which is not what I need for the report. I need the output to be in datetime/time format which can give me results like '40:00:00' for the above example.
I hope I am able I have provided all the information. Please help as I am struggling with this from many days.
Expected Output: 40:00:00
Maybe try CONVERT style 108.
SELECT CONVERT(varchar(10),convert(varchar,(#WorkTimeInSeconds)/3600)
+ ':' + right( '0' + convert(varchar,((#WorkTimeInSeconds) %3600)/60),2)
+ ':' + right( '0' + convert(varchar,(#WorkTimeInSeconds) %60),2),108)

Round off Value in sql query

I need to round off my value with some constraints. For example..
1. Value - 5.89
Required solution - 6.29
It's time difference value, which needs be rounded after every decimal value as 0.60. whenever, my decimal values reached at 60, it should rounded to the next value and remaining decimal value above 60, needs to be as it is.
Please suggest.
Its working for me in MS ACCESS
select Format(YourField \ 60, '0') & '.' & Format(YourField Mod 60, '00') as [YourField] from YourTable
for SQL SERVER Try This,
select Right('0' + cast(YourField / 60) %60 as varchar),2)+ ':'+
right('0' + cast(YourField %60 as varchar),2 ) from YourTable

Select length as minute:seconds format

Trying to convert seconds to a minute:seconds format.
e.g. 207 seconds would be 3:27
I have a table with column length that has the length of songs stored in seconds.
Using this query almost works, however, when a song should be 3:03 it will instead show 3:3
select concat(Length/60, ':', Length%60) as Length from songs
SELECT Convert(nvarchar, (Length/60)) + ':' + RIGHT('0' + Convert(nvarchar, Length%60), 2) as Length from songs
I build this using a varaible since I didn't have your table, but try this:
declare #length int = 207
select concat(#Length/60, ':', case len(#Length%60) when 1 then '0' else '' end, #Length%60) as [leng]
You can use dateadd to do that:
select convert(varchar, dateadd(second, 187, 0), 108)
This returns "00:03:07" so you can cut away hours if you don't need them.