简体   繁体   English

将分钟舍入到小时和刻钟 (SQL)

[英]Round minutes to hours and quarters of hours (SQL)

I have a total of minutes and I am trying to convert these minutes in hours and quarters of hours, for instance:我总共有几分钟,我试图将这些分钟转换为几小时和几刻钟,例如:

513 minutes / 60 = 8.55 -> so would be 8.3 hours
485 minutes / 60 = 8.083 -> 8 hours

How can I do this using SQL?如何使用 SQL 做到这一点?

I have been searching and what I found was this, but doesn't work for me:我一直在搜索,我发现的是这个,但对我不起作用:

@mins is the variable where I am storing the minutes. @mins是我存储分钟的变量。

set @mins = dateadd(minute, datediff(minute,0,@mins) / 15 * 15, 0)

Assuming that you can't have times that are 24 or more hours, you could do something like:假设您不能有 24 小时或更长时间的时间,您可以执行以下操作:

SELECT CONCAT(DATEPART(HOUR,T.[Time]),'.',DATEPART(MINUTE,T.[Time]) /10)
FROM (VALUES(513),(485),(107))V(YourColumn)
     CROSS APPLY(VALUES(DATEADD(MINUTE, YourColumn, CONVERT(time(0),'00:00')))) T([Time]);

But, like I mention in my comment under the question, this feels like the beginnings of an XY problem .但是,就像我在问题下的评论中提到的那样,这感觉就像是XY 问题的开始。

This appears to do what you want, rounding down to the nearest 15 minutes and then treating that as "decimal" hours:这似乎可以满足您的要求,四舍五入到最接近的 15 分钟,然后将其视为“十进制”小时:

select mins / 60 + (((mins % 60)) / 15) * 0.15
from (values (513), (485)) v(mins)

Using a number to represent the minutes is really misleading and likely to result in errors downstream, when 8.3 is interpreted as 8.3 hours -- which is 498 minutes.8.3被解释为 8.3 小时(即 498 分钟)时,使用数字表示分钟确实会产生误导,并且可能会导致下游错误。

I would strongly encourage you to represent the value as a proper time value.我强烈建议您将该值表示为适当的time值。 You can generate that using:您可以使用以下方法生成:

timefromparts(mins / 60, (((mins % 60)) / 15) * 15, 0, 0, 0)

Here is a db<>fiddle. 是一个 db<>fiddle。

DECLARE @MinutesTotal   int
DECLARE @QuatersTotal   int
DECLARE @HoursOut       int
DECLARE @QuatersOut     numeric(10,2)

SET @MinutesTotal   =   
                --      513 --  8:30 
                --      485 --  8:00 
                        166 --  2:45    

SET @QuatersTotal   =   @MinutesTotal / 15                          
SET @HoursOut       =   @QuatersTotal /  4                          
SET @QuatersOut     =   (@QuatersTotal - (@HoursOut * 4)) * 15



DECLARE @ResultTime time = '00:00'

SET     @ResultTime = DATEADD(HOUR  , @HoursOut     , @ResultTime)
SET     @ResultTime = DATEADD(MINUTE, @QuatersOut   , @ResultTime)

SELECT  
        @ResultTime 
    ,   ResultAsString  =   CONVERT(varchar(5), @ResultTime )
    ,   ResultAsDecimal =   @HoursOut + (@QuatersOut / 100)

I think that you want this arithmetics:我认为你想要这个算术:

select @min / 60 + @min % 60 / 100.0

This gives you the number of hours as an integer, followed by the remaining number of minutes in the decimal part, so for your sample data this yields 8.33 and 8.05 respectively.这为您提供了 integer 的小时数,然后是小数部分的剩余分钟数,因此对于您的示例数据,这分别产生8.338.05

On the other hand, if you want the decimal part to represents the number of quarters of hours, then:另一方面,如果您希望小数部分表示小时数,则:

select @min / 60 + @min % 60 / 15 / 10.0

This produces 8.2 and 8.0 .这会产生8.28.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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