尝试将 SQL 中的太平洋时间转换为 UTC,在使用 datetime 声明变量时将 运行 保持为错误

Trying to convert pacific time to UTC in SQL, keep running into error when declaring variable using datetime

我正在尝试获取正确的日期结果,但我想获取 UTC 时间以便我的数据正确。我想使用 UTC 时间获得星期六的正确结果,我应该使用 getdate() 吗?

这是部分查询:

declare @STARTDATE datetime = '07-03-2021 00:00:01'
declare @ENDDATE datetime = '07-03-2021 23:59:59'

select dateadd(hh, datediff(hh, getutcdate(), getdate()), @STARTDATE) as utc_stardate,dateadd(hh, datediff(hh, getutcdate(), getdate()), @endDATE) as utc_enddate
where repackagingtime between utc_stardate and utc_enddate

错误说:

Must declare scalar variable

我该如何解决?

更新:

我没有先声明,但我应该设置为varchar吗?

像这样?

declare @utc_enddate datetime

但我应用更改后,每天的数据现在都是空白

将 utc 日期变量声明为 DATE 数据类型

您可以像这样将时区转换为 utc(est 到 utc)。只需将 getdate() 替换为您的可变日期即可。

DECLARE @UTCSTART DATE

SET @UTCSTART = CONVERT(DATE, GETDATE() AT TIME ZONE 'Eastern Standard Time' AT TIME ZONE 'UTC')