Convert decimal time to hours and minutes

前端 未结 4 2040
刺人心
刺人心 2020-12-03 15:35

Been struggling with this and can\'t seem to find the right answer, although there are plenty of mentions for converting, but nothing specific is working.

I need to

相关标签:
4条回答
  • 2020-12-03 15:56

    You can try:

    DECLARE @HOURS decimal(7,4) = 20.5599
    SELECT  CAST(CONVERT(VARCHAR,DATEADD(SECOND, @HOURS * 3600, 0),108) AS TIME)
    

    output : 20:33:35

    But remember : Type Time in MSSQL only under 24hrs

    If you want greater than 24hrs, try:

    DECLARE @HOURS decimal(7,4) = 25.5599
    SELECT 
    RIGHT('0' + CAST (FLOOR(@HOURS) AS VARCHAR), 2) + ':' + 
    RIGHT('0' + CAST(FLOOR((((@HOURS * 3600) % 3600) / 60)) AS VARCHAR), 2) + ':' + 
    RIGHT('0' + CAST (FLOOR((@HOURS * 3600) % 60) AS VARCHAR), 2)
    

    output : 25:33:35

    -- Update

    Decimal minutes to more than 24hrs

    DECLARE @MINUTES decimal(7,4) = 77.9
    SELECT
    RIGHT('0' + CAST (FLOOR(COALESCE (@MINUTES, 0) / 60) AS VARCHAR (8)), 2) + ':' + 
    RIGHT('0' + CAST (FLOOR(COALESCE (@MINUTES, 0) % 60) AS VARCHAR (2)), 2) + ':' + 
    RIGHT('0' + CAST (FLOOR((@MINUTES* 60) % 60) AS VARCHAR (2)), 2);
    

    output: 01:17:54

    0 讨论(0)
  • 2020-12-03 16:03
    DECLARE @f FLOAT = 13.5;
    
    SELECT CONVERT(TIME(0), DATEADD(MINUTE, 60*@f, 0));
    

    Or if you just want hh:mm as a string:

    SELECT CONVERT(CHAR(5), DATEADD(MINUTE, 60*@f, 0), 108);
    

    Just be careful if you have values >= 24.

    0 讨论(0)
  • This should work for you

    DECLARE @f [real]
    SET @f = 13.50
    
    SELECT DATEADD(mi, (@f - FLOOR(@f)) * 60, DATEADD(hh, FLOOR(@f), CAST ('00:00:00' AS TIME)))
    
    0 讨论(0)
  • 2020-12-03 16:22

    How about you convert to minutes and add to the 00:00 time like so:

    DECLARE @c datetime
    select @c = dateadd(mi,fdsViewTimesheet.perStandardHours*60,'00:00')  
    

    If you wanted to do it in the statement with Time only:

     select CONVERT(TIME,dateadd(mi,fdsViewTimesheet.perStandardHours*60,'00:00')  )
    

    If you have values that are larger than 24 hours, then the standard datetime and time types in sql cannot hold these. They are limited to holding 24 hour ranges. What you would need to do is store the time representation in a string for example like so:

    select cast(floor(fdsViewTimesheet.perStandardHours) as varchar(10)) + ':' + cast(FLOOR( (fdsViewTimesheet.perStandardHours - floor(fdsViewTimesheet.perStandardHours))*60)as varchar(2))
    
    0 讨论(0)
提交回复
热议问题