【问题标题】:Retrieve a record from table with particular datetime从具有特定日期时间的表中检索记录
【发布时间】:2020-10-06 16:33:01
【问题描述】:

我需要在 SQL Server 中检索具有特定日期时间的记录。尝试了许多选项,例如

select *
from test
where convert(datetime ,imported_date) = convert(datetime,'2020-06-16 
22:00:07')

imported_date 是 datetime2 数据类型。 小于和大于工作但等于条件失败。请帮忙

【问题讨论】:

  • datetime2 比 datetime 更精确。那么您尝试选择的行中imported_date 列的确切值是多少?您可能会遇到这个datetime to datetime2 转换问题。编辑:MS 破坏了他们所有的连接链接真的很臭

标签: sql-server date jpa select where-clause


【解决方案1】:

您的日期时间可能有小数秒。如果是这样,我建议将其表述为:

where 
    imported_date >= '2020-06-16 22:00:07' 
    and imported_date < '2020-06-16 22:00:08'

这比在 datetime 列上使用转换函数更有效,因为它允许数据库使用该列上的索引(如果有)。

如果您正在使用参数:

where 
    imported_date >= @param_date
    and imported_date < dateadd(second, 1, @param_date)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2013-10-07
    • 2015-01-27
    相关资源
    最近更新 更多