Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从Convert(date,DueDate)=@today的项目中选择*_Sql_Sql Server_Sql Server 2017 - Fatal编程技术网

Sql 从Convert(date,DueDate)=@today的项目中选择*

Sql 从Convert(date,DueDate)=@today的项目中选择*,sql,sql-server,sql-server-2017,Sql,Sql Server,Sql Server 2017,我有一个简单的选择问题。存储DueDate的数据库字段是DateTimeOffset declare @today Date = GetDate(); Select * from Items where Convert(date, DueDate) = @today 这似乎一直工作到深夜,此时它返回第二天实际到期的行 问题:GetDate,因为它是运行在Azure上的Sql Server,实际上返回GetUTCDate。因此,在美国东部时间晚上9点,它返回的日期不是今天,而是明天,因为UT

我有一个简单的选择问题。存储DueDate的数据库字段是DateTimeOffset

 declare @today Date = GetDate();
 Select * from Items where Convert(date, DueDate) = @today
这似乎一直工作到深夜,此时它返回第二天实际到期的行

问题:GetDate,因为它是运行在Azure上的Sql Server,实际上返回GetUTCDate。因此,在美国东部时间晚上9点,它返回的日期不是今天,而是明天,因为UTC时间比次日凌晨2点提前5小时

我把@today定为一个日期,以便忽略时间部分

但在数据库中,DueDate为2018-12-02 05:00:00.0000000+00:00,但如果将其转换为日期,则日期为2018-12-02,但@today为2018-12-03


< P> >我应该如何编写这个SQL?< /p> < p>你需要把今天放到一个时区中,考虑下面的2个查询:注意今天是如何设置的:
dbfiddle

我会将日期传递到查询中,而不是使用getdate,因为即使您可以根据需要调整日期,也必须考虑夏令时,如果您将数据库移动到其他服务器,则日期将发生更改。有几种方法可以更改时区。SWITCHOFFSET自2008年起开始工作,SQL Server 2016+的较新版本在时区具有。什么是[DueDate]的数据类型?请参见@Used\u By\u,DueDate是DateTimeOffset。非常感谢您的帮助。很高兴,请注意我是如何处理要筛选的谓词的,我认为这在这里特别重要,但您应该尽量避免将列更改为今天,改为使用范围。
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