简体   繁体   English

T-SQL:将时间四舍五入到15分钟,但仅在15分钟后5分钟之后

[英]T-SQL: Rounding Time in to 15 minutes but only after 5 minutes past the 15min

I have a table with a datetime field. 我有一个带有日期时间字段的表。 I'm trying to round up/down the time in 15 minute intervals. 我正在尝试以15分钟为间隔四舍五入时间。 But with non standard mathematical rounding rules where rounding up happens if its greater than 5 minutes past the 15 minute interval. 但是对于非标准的数学舍入规则,如果超过15分钟间隔超过5分钟,则会进行舍入。

For example 例如

IF 06:05 round down to 06:00 如果06:05向下舍入到06:00

IF 06:06 round up to 06:15 IF 06:06向上舍入到06:15

IF 06:20 round down to 06:15 如果06:20向下舍入到06:15

IF 06:21 round up to 06:30 and so on.. IF 06:21向上舍入到06:30,依此类推。

I've managed to find here T-SQL: Round to nearest 15 minute interval to round the nearest 15 minutes but this uses mathematical rounding meaning 06:07 would still round down to 06:00 instead of rounding up to 06:15. 我设法在这里找到T-SQL:舍入到最近的15分钟间隔以舍入到最近的15分钟,但这使用了数学舍入,这意味着06:07仍将舍入到06:00,而不是舍入到06:15。

Below code where i've got to: 下面的代码我必须去:

cast(datepart(hour, getdate()) + datepart(minute, getdate()) / 60.00 as decimal(5, 2))

Just use a couple of date tricks. 只需使用一些约会技巧即可。

This code will give you the top of the hour for the time you're evaluating (minutes effectively removed by adding up the hours since the 0 date in SQL): 这段代码将为您提供评估时间的最佳时间(通过累加SQL中的0日期以来的小时数,有效地消除了分钟数):

select dateadd(hour, datediff(hour, 0, getdate()), 0)

From there, you need a CASE expression to evaluate which quartile of the hour the time in question falls into (just a snippet here): 从那里开始,您需要一个CASE表达式来评估所讨论的时间属于哪个小时的四分位数(此处只是一个片段):

  case 
    when datepart(minute, dtm) > 50 then 60
    when datepart(minute, dtm) > 35 then 45
    when datepart(minute, dtm) > 20 then 30
    when datepart(minute, dtm) > 5  then 15
    else 0
  end

Put those two pieces together with a DATEADD to decide how many minutes we're adding to that even hour mark: 将这两部分与DATEADD放在一起,以决定我们将添加到该偶数小时标记的分钟数:

declare @dtms table (dtm datetime);
insert @dtms (dtm)
values ('2019-07-16T12:05:00'),
       ('2019-07-16T12:06:00'),
       ('2019-07-16T12:21:00'),
       ('2019-07-16T12:29:00'),
       ('2019-07-16T12:35:00'),
       ('2019-07-16T12:38:00'),
       ('2019-07-16T12:56:00')

select
  dtm,
  dateadd(minute, 
    case 
      when datepart(minute, dtm) > 50 then 60
      when datepart(minute, dtm) > 35 then 45
      when datepart(minute, dtm) > 20 then 30
      when datepart(minute, dtm) > 5  then 15
      else 0
    end, dateadd(hour, datediff(hour, 0, dtm), 0)) as rounded
from @dtms;

Results: 结果:

+-------------------------+-------------------------+
|           dtm           |         rounded         |
+-------------------------+-------------------------+
| 2019-07-16 12:05:00.000 | 2019-07-16 12:00:00.000 |
| 2019-07-16 12:06:00.000 | 2019-07-16 12:15:00.000 |
| 2019-07-16 12:21:00.000 | 2019-07-16 12:30:00.000 |
| 2019-07-16 12:29:00.000 | 2019-07-16 12:30:00.000 |
| 2019-07-16 12:35:00.000 | 2019-07-16 12:30:00.000 |
| 2019-07-16 12:38:00.000 | 2019-07-16 12:45:00.000 |
| 2019-07-16 12:56:00.000 | 2019-07-16 13:00:00.000 |
+-------------------------+-------------------------+

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

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