简体   繁体   中英

How to format time from dd:hh:mm:ss to only hh:mm:ss in SQL server?

I found SQL function which get second as input parameter and return seconds in dd:hh:mm:ss format eg for 93600 seconds it returns 1:02:00:00

it means 1 day 2 hours 0 minutes and 0 seconds.

Function that i used is :

FUNCTION [dbo].[udfTimeSpanFromSeconds]
(
    @Seconds int
)
RETURNS varchar(15)
AS
BEGIN
DECLARE 
    --Variable to hold our result
      @DHMS varchar(15)
    --Integers for doing the math
    , @Days int --Integer days
    , @Hours int --Integer hours
    , @Minutes int --Integer minutes
    --Strings for providing the display
    , @sDays varchar(5) --String days
    , @sHours varchar(2) --String hours
    , @sMinutes varchar(2) --String minutes
    , @sSeconds varchar(2) --String seconds

--Get the values using modulos where appropriate
SET @Hours = @Seconds/3600
SET @Minutes = (@Seconds % 3600) /60
SET @Seconds = (@Seconds % 3600) % 60

--If we have 24 or more hours, split the @Hours value into days and hours
IF @Hours > 23 
BEGIN
    SET @Days = @Hours/24
    SET @Hours = (@Hours % 24)
END
ELSE
BEGIN
    SET @Days = 0
END

--Now render the whole thing as string values for display
SET @sDays = convert(varchar, @Days)
SET @sHours = RIGHT('0' + convert(varchar, @Hours), 2)
SET @sMinutes = RIGHT('0' + convert(varchar, @Minutes), 2)
SET @sSeconds = RIGHT('0' + convert(varchar, @Seconds), 2)

--Concatenate, concatenate, concatenate
SET @DHMS =  @sDays + ':' + @sHours + ':' + @sMinutes + ':' + @sSeconds

RETURN @DHMS

END

and select command that will retrieve output is

select dbo.udfTimeSpanFromSeconds('93600' )

it shows me result as:

在此处输入图片说明

Now i need this output in hh:mm:ss format eg for current example 26:00:00 it means 26 hours 0 minutes and 0 seconds.

I am using SQL server 2008. Thanks in advance.

You can do this with math

DECLARE @sec INT = 93600

SELECT
    CONVERT(VARCHAR(10), (@sec / 3600)) + ':' +
    RIGHT('0' + CONVERT(VARCHAR(2), ((@sec % 3600) / 60)), 2) + ':' +
    RIGHT('0' + CONVERT(VARCHAR(2), (@sec % 60)), 2)

Written as a function:

CREATE FUNCTION udfTimeSpanFromSeconds(
    @sec INT
)
RETURNS VARCHAR(15)
AS
BEGIN
RETURN 
    CONVERT(VARCHAR(10), (@sec / 3600)) + ':' +
    RIGHT('0' + CONVERT(VARCHAR(2), ((@sec % 3600) / 60)), 2) + ':' +
    RIGHT('0' + CONVERT(VARCHAR(2), (@sec % 60)), 2)
END

Sample call:

SELECT dbo.udfTimeSpanFromSeconds(360000)

RESULT:

100:00:00

If you want you function to return hh:mm:ss then it needs to be as below. This does however limit the total time to be less than 100 hours. You can fix this by increasing the length of the Hours String by changing the right clause and increasing the length of the string returned, as I have now done to illustrate.

(Normally once you have summed your time you usually divide the total by 3600.00 to produce a decimal value for use in further calculations, for example if you are paying by the hour)

FUNCTION [dbo].[udfTimeSpanFromSeconds]
(
    @Seconds int
)
RETURNS varchar(10)
AS
BEGIN
DECLARE 
    --Variable to hold our result
      @HMS varchar(15)
    --Integers for doing the math
    , @Hours int --Integer hours
    , @Minutes int --Integer minutes
    --Strings for providing the display
    , @sHours varchar(2) --String hours
    , @sMinutes varchar(2) --String minutes
    , @sSeconds varchar(2) --String seconds

--Get the values using modulos where appropriate
SET @Hours = @Seconds/3600
SET @Minutes = (@Seconds % 3600) /60
SET @Seconds = (@Seconds % 3600) % 60

--Now render the whole thing as string values for display
SET @sHours = RIGHT('0' + convert(varchar(5), @Hours), 3)
SET @sMinutes = RIGHT('0' + convert(varchar(3), @Minutes), 2)
SET @sSeconds = RIGHT('0' + convert(varchar(3), @Seconds), 2)

--Concatenate, concatenate, concatenate
SET @HMS =  @sHours + ':' + @sMinutes + ':' + @sSeconds

RETURN @HMS

END

If you want to return 100 hours then you have to use it:-

FUNCTION [dbo].[udfTimeSpanFromSeconds]
(
    @Seconds int
)
RETURNS varchar(8)
AS
BEGIN
DECLARE 
--Variable to hold our result
  @HMS varchar(15)
--Integers for doing the math
, @Hours int --Integer hours
, @Minutes int --Integer minutes
--Strings for providing the display
, @sHours varchar(3) --String hours
, @sMinutes varchar(2) --String minutes
, @sSeconds varchar(2) --String seconds

--Get the values using modulos where appropriate

SET @Hours = @Seconds/3600
SET @Minutes = (@Seconds % 3600) /60
SET @Seconds = (@Seconds % 3600) % 60

--Now render the whole thing as string values for display
SET @sHours = RIGHT('0' + convert(varchar, @Hours), 3)
SET @sMinutes = RIGHT('0' + convert(varchar, @Minutes), 2)
SET @sSeconds = RIGHT('0' + convert(varchar, @Seconds), 2)

--Concatenate, concatenate, concatenate
SET @HMS =  @sHours + ':' + @sMinutes + ':' + @sSeconds

RETURN @HMS

END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM