Implementando um esquema de partição rotativa, por kejser.org/table-pattern-rotating-log-ring- . Tive um problema com DATEDIFF arredondando valores:
DECLARE @Partitions INT = 15;
SELECT
a1.dt
, dtTrunc
, dtDiff
, PartitionKey = CAST(DATEDIFF(DAY, 0, dtDiff) % @Partitions AS TINYINT)
FROM
(
VALUES
('2024-08-17 23:59:59.997')
, ('2024-08-17 23:59:59.998')
, ('2024-08-17 23:59:59.999')
, ('2024-08-18 00:00:00.000')
)
AS v(dt)
CROSS APPLY
(
SELECT
dt = CAST(v.dt AS DATETIME2(3))
) a1
CROSS APPLY
(
SELECT
dtTrunc = CAST(a1.dt AS DATE)
, dtDiff = DATEDIFF(day, 0, a1.dt)
) a2
Problema resolvido com elenco até o momento:
DECLARE @Partitions INT = 15;
SELECT
a1.dt
, dtTrunc
, dtDiff
, PartitionKey = CAST(DATEDIFF(DAY, 0, dtDiff) % @Partitions AS TINYINT)
FROM
(
VALUES
('2024-08-17 23:59:59.997')
, ('2024-08-17 23:59:59.998')
, ('2024-08-17 23:59:59.999')
, ('2024-08-18 00:00:00.000')
)
AS v(dt)
CROSS APPLY
(
SELECT
dt = CAST(v.dt AS DATETIME2(3))
) a1
CROSS APPLY
(
SELECT
dtTrunc = CAST(a1.dt AS DATE)
, dtDiff = DATEDIFF(day, 0, CAST(a1.dt AS DATE))
) a2
Esse é um comportamento esperado/documentado? Se sim, onde?