sql - SQL Server 时区在 where 子句中
问题描述
我正在使用 UTC 时间存储日期时间字段。我们需要使用 CST 时区过滤记录。我试过这个查询:
select id, CreatedOn,
CreatedOn AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time' AS LocalTime
from Status
WHERE CAST((CreatedOn AT TIME ZONE 'Central Standard Time') AS date) = '2018-09-06'
order by CreatedOn desc;
问题在于,当 UTC 时间更改为 9 月 6 日时,它还带来了在 CST 时间 9 月 5 日晚上保存的那些记录。在 CST 时间仅过滤掉 9 月 6 日记录的正确方法是什么?
解决方案
我发现查询存在问题,我在 where 子句中缺少 'UTC' AT TIME ZONE。这是有效的正确查询:
select id, CreatedOn,
CreatedOn AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time' AS LocalTime from CosmoStatus WHERE CAST((CreatedOn AT TIME ZONE
'UTC' AT TIME ZONE 'Central Standard Time') AS date) = '2018-09-06'
order by CreatedOn desc;
推荐阅读
- php - Laravel Event Sourcing (Spatie) - 如何处理更复杂的业务规则
- java - 在 .map() 与 Reactor 中的 .flatMap() 中返回 null
- sql-server - .Net Core 读取 SQL 数据库返回空值
- algorithm - A* 算法 - 起点
- matlab - 如何让皮肤检测运行
- c# - 遇到 C# 和 mysql 的问题
- google-apps-script - 与谷歌应用程序脚本共享文件夹而不收到电子邮件
- java - Prime 面向 Treetable 内的自定义数据表(Matser 详细数据)
- c++ - C++ VS 工具集 141 - 142 铸造差异
- architecture - 可组合架构是否与 PaaS 相同