假设我有一个时间戳,并且我想将此时间戳截断为自时间戳所在的星期日起的分钟数。在标准 SQL 中,我会执行以下操作:
timestamp_diff(created_at, timestamp_trunc(created_at, week), minute)
其中 thecreared_at
将是我拥有的时间戳,并且week
将是 truncate 函数将其截断为周的参数。这在 Azure 中如何实现?
我将在假设“时间戳”是指日期时间值的情况下回答这个问题。Sql Server(和 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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句