Select * 来自 Convert(date, DueDate) = @today 的项目
Select * from Items where Convert(date, DueDate) = @today
我在使用简单的 select 时遇到了问题。存储 DueDate 的数据库字段是 DateTimeOffset。
declare @today Date = GetDate();
Select * from Items where Convert(date, DueDate) = @today
这似乎工作到深夜,此时 returns 行实际上是第二天到期的。
问题:GetDate(),因为它是 Azure 上的 Sql 服务器 运行,实际上是 returns GetUTCDate()。所以在美国东部时间晚上 9 点,returns 不是今天,而是明天,因为 UTC 时间提前 5 小时(凌晨 2 点,第二天)。
我已将 @today
设为日期,以便可以忽略时间部分。
但是在数据库中,DueDate是2018-12-02 05:00:00.0000000 +00:00
但是当这个转换为日期时,日期是2018-12-02
,但是@today
是2018-12-03
那我应该怎么写SQL?
您需要将“今天”放入时区,考虑下面的 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<>fiddle here
我在使用简单的 select 时遇到了问题。存储 DueDate 的数据库字段是 DateTimeOffset。
declare @today Date = GetDate();
Select * from Items where Convert(date, DueDate) = @today
这似乎工作到深夜,此时 returns 行实际上是第二天到期的。
问题:GetDate(),因为它是 Azure 上的 Sql 服务器 运行,实际上是 returns GetUTCDate()。所以在美国东部时间晚上 9 点,returns 不是今天,而是明天,因为 UTC 时间提前 5 小时(凌晨 2 点,第二天)。
我已将 @today
设为日期,以便可以忽略时间部分。
但是在数据库中,DueDate是2018-12-02 05:00:00.0000000 +00:00
但是当这个转换为日期时,日期是2018-12-02
,但是@today
是2018-12-03
那我应该怎么写SQL?
您需要将“今天”放入时区,考虑下面的 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<>fiddle here