2015-11-26 122 views
2

我在SQL Server中遇到了一個舍入爲datetime的問題。我得到datetimerec_datetime,但我想圍繞這個datetime在一個新的列r_datetime,必須四捨五入到最近的15分鐘,整列rec_datetime在SQL Server中將日期時間舍入到最近的15分鐘

例子:

  • [2015-11-24 19:06:00.000] - 預期的結果 - >[2015-11-24 19:00:00.000]
  • [2015-11-24 19:09:00.000] - 預期的結果 - >[2015-11-24 19:15:00.000]

是否有可能通過選擇舍它整列?喜歡的東西:

select round(rec_datetime....... 
+3

的可能的複製[如何輪T-SQL一個時間](http://stackoverflow.com/questions/249794/how-to-round-a-time -in-T-SQL) – Paddy

回答

2

嘗試這樣:

SELECT 
    dateadd(minute, datediff(minute, '1999-12-31 23:52:30', col)/15*15, '2000-01-01') 
FROM (values('2015-11-24 19:06:00.000'),('2015-11-24 19:09:00.000')) x(col) 

結果:

2015-11-24 19:00:00.000 
2015-11-24 19:15:00.000 
0

在這種情況下,你需要四捨五入分鐘到最近的15

嘗試查詢像這個:

SELECT 

original_datetime 

--: this will give you the minute part 
, datepart(minute, original_datetime) AS minuteFromDate 

--: now get the nearest minute part to 15min, round it nearest to 15 
, round(datepart(minute, original_datetime) * 1.0/15 , 0) * 15.0 AS roundedToNearest15 

-- now remove the minute from original datetime, & add the rounded minute to the resultant datetime value 
-- this will give you expected result 
, dateadd(minute 
      , (round(datepart(minute, original_datetime) * 1.0/15 , 0) * 15.0) 
      , dateadd(minute, -datepart(minute, original_datetime), original_datetime) 
      ) as rec_datetime 

FROM (values('2015-11-24 19:06:00.000') 
      ,('2015-11-24 19:09:00.000') 
     ,('2015-11-24 19:56:00.000') 
     ,('2015-11-24 19:48:00.000') 
    ) x(original_datetime) 
4

下舍入,圓最近&圍捕到最近的15分鐘

DATEADD(minute, (DATEDIFF(minute, 0,         dateTimeX )/15) * 15, 0) AS dateTimeRoundDown 
DATEADD(minute, (DATEDIFF(minute, 0, DATEADD(second, (15 * 60)/2, dateTimeX))/15) * 15, 0) AS dateTimeRoundNearest 
DATEADD(minute, (DATEDIFF(minute, 0, DATEADD(minute, 15   , dateTimeX))/15) * 15, 0) AS dateTimeRoundUp 

回合下來

DATEADD(minute, (DATEDIFF(minute, 0, dateTimeX)/15) * 15, 0) AS dateTimeRoundDown 

獲得以分鐘爲單位的偏移量(分鐘數,因爲基地日期):

DATEDIFF(minute, 0, dateTimeX) 

圓降至15分鐘塊由整數分割:

DATEDIFF(minute, 0, dateTimeX)/15) * 15 

添加基追溯到以分鐘:

DATEADD(minute, (DATEDIFF(minute, 0, dateTimeX)/15) * 15, 0) 

回合最近

DATEADD(minute, (DATEDIFF(minute, 0, DATEADD(second, (15 * 60)/2, dateTimeX))/15) * 15, 0) AS dateTimeRoundNear 

2分之15分鐘被添加到偏移量。

由於整數除法需要秒數。

圍捕

DATEADD(minute, (DATEDIFF(minute, 0, DATEADD(minute, 15, dateTimeX))/15) * 15, 0) AS dateTimeRoundUp 

15分鐘被添加到偏移

基準日期

我一般使用0基準日,其是SQL Server的 '曆元'

SELECT DATEADD(minute, 0, 0) -- '1900-01-01 00:00:00.000' 

由於DATEADD()& DATEDIFF()對參數使用INT(32位)的SQL Server數據類型,因此對於日期很遠的將來,這可能會導致溢出。

使用另一個固定日期,例如'2010-01-01',將避免溢出。

所選擇的基本日必須具有的00:00:00

時間部分使用基日期和整數除法,沒有鑄造&不需要浮點運算。

單元測試

DECLARE @start DATETIME = '2017-04-20 21:00:00' 
DECLARE @end DATETIME = '2017-04-20 22:00:00' 

;WITH CTE_dateTimes AS 
(
    SELECT @start AS dateTimeX 
    UNION ALL 
    SELECT DATEADD(minute, 1, dateTimeX) 
    FROM CTE_dateTimes 
    WHERE DATEADD(minute, 1, dateTimeX) <= @end 
) 
SELECT dateTimeX, 
    DATEADD(minute, (DATEDIFF(minute, 0,         dateTimeX )/15) * 15, 0) AS dateTimeRoundDown, 
    DATEADD(minute, (DATEDIFF(minute, 0, DATEADD(second, (15 * 60)/2, dateTimeX))/15) * 15, 0) AS dateTimeRoundNearest, 
    DATEADD(minute, (DATEDIFF(minute, 0, DATEADD(minute, 15   , dateTimeX))/15) * 15, 0) AS dateTimeRoundUp 

FROM CTE_dateTimes 
相關問題