我在进行简单选择时遇到了麻烦。存储DueDate的数据库字段是DateTimeOffset。
declare @today Date = GetDate();
Select * from Items where Convert(date, DueDate) = @today
这似乎一直工作到深夜,这时它返回第二天实际上应该到期的行。
问题:GetDate(),因为它是在Azure上运行的Sql Server,实际上返回了GetUTCDate()。因此,在美国东部标准时间晚上9点,它返回的日期不是今天,而是明天,因为UTC时间提前5小时(第二天,凌晨2点)。
我已经将@today
设为日期,以便可以忽略时间部分。
但是在数据库中,DueDate是2018-12-02 05:00:00.0000000 +00:00
,但是当将其强制转换为日期时,日期是2018-12-02
,而@today
是2018-12-03
那么我应该如何编写此SQL?
答案 0 :(得分:1)
您需要将“今天”放置在一个时区中,请考虑以下2个查询(注意如何设置“今天”),请参见AT TIME ZONE:
declare @today as datetimeoffset; SET @today = cast(getdate() as date); ;with Items as ( select -24 as n, dateadd(hour,-24,SYSDATETIMEOFFSET()) as DueDate union all select n+1, dateadd(hour,n+1,SYSDATETIMEOFFSET()) from Items where n < 24 ) Select * , @today from Items where DueDate >= @today and DueDate < dateadd(day,1,@today) ; GO
n | DueDate | (No column name) --: | :------------------------- | :------------------------- -10 | 03/12/2018 00:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 -9 | 03/12/2018 01:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 -8 | 03/12/2018 02:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 -7 | 03/12/2018 03:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 -6 | 03/12/2018 04:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 -5 | 03/12/2018 05:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 -4 | 03/12/2018 06:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 -3 | 03/12/2018 07:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 -2 | 03/12/2018 08:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 -1 | 03/12/2018 09:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 0 | 03/12/2018 10:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 1 | 03/12/2018 11:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 2 | 03/12/2018 12:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 3 | 03/12/2018 13:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 4 | 03/12/2018 14:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 5 | 03/12/2018 15:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 6 | 03/12/2018 16:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 7 | 03/12/2018 17:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 8 | 03/12/2018 18:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 9 | 03/12/2018 19:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 10 | 03/12/2018 20:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 11 | 03/12/2018 21:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 12 | 03/12/2018 22:57:54 +00:00 | 03/12/2018 00:00:00 +00:00 13 | 03/12/2018 23:57:54 +00:00 | 03/12/2018 00:00:00 +00:00
declare @today as datetimeoffset; SET @today = cast(cast(getdate() as date) as datetimeoffset) AT TIME ZONE 'Pacific Standard Time'; ;with Items as ( select -24 as n, dateadd(hour,-24,SYSDATETIMEOFFSET()) as DueDate union all select n+1, dateadd(hour,n+1,SYSDATETIMEOFFSET()) from Items where n < 24 ) Select * , @today from Items where DueDate >= @today and DueDate < dateadd(day,1,@today) ; GO
n | DueDate | (No column name) --: | :------------------------- | :------------------------- -10 | 03/12/2018 00:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 -9 | 03/12/2018 01:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 -8 | 03/12/2018 02:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 -7 | 03/12/2018 03:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 -6 | 03/12/2018 04:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 -5 | 03/12/2018 05:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 -4 | 03/12/2018 06:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 -3 | 03/12/2018 07:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 -2 | 03/12/2018 08:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 -1 | 03/12/2018 09:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 0 | 03/12/2018 10:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 1 | 03/12/2018 11:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 2 | 03/12/2018 12:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 3 | 03/12/2018 13:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 4 | 03/12/2018 14:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 5 | 03/12/2018 15:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 6 | 03/12/2018 16:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 7 | 03/12/2018 17:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 8 | 03/12/2018 18:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 9 | 03/12/2018 19:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 10 | 03/12/2018 20:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 11 | 03/12/2018 21:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 12 | 03/12/2018 22:57:54 +00:00 | 02/12/2018 16:00:00 -08:00 13 | 03/12/2018 23:57:54 +00:00 | 02/12/2018 16:00:00 -08:00
db <>提琴here