在 Microsoft Azure SQL 中截断日期?

Truncating dates in Microsoft Azure SQL?

假设我有一个时间戳,我想将该时间戳截断为自该时间戳所在的星期日以来的分钟数。在标准 SQL 中,我会执行类似以下操作:

timestamp_diff(created_at, timestamp_trunc(created_at, week), minute) 其中 creared_at 将是我拥有的时间戳,而 week 将是截断函数将其截断到一周的参数。这在 Azure 中如何实现?

我将假设 "timestamp" 您指的是日期时间值来回答这个问题。 Sql 服务器(和 Azure)中的实际时间戳数据类型是 rowversion 数据类型的已弃用同义词,与实际日期或时间无关。

如果您处理的是日期时间值,则以下内容将为您提供 @timestamp_to_truncate 与上周日凌晨 12 点之间的分钟数:

declare @timestamp_to_truncate datetime = getdate()

declare @sunday datetime = cast(dateadd(day,1-DATEPART(dw, @timestamp_to_truncate),cast(@timestamp_to_truncate as date)) as datetime)

select datediff(mi,@sunday,@timestamp_to_truncate)