【问题标题】:SQL Server, HQL: How to compare SQL server datetime column field to dateSQL Server,HQL:如何将 SQL Server 日期时间列字段与日期进行比较
【发布时间】:2016-10-08 11:37:10
【问题描述】:

我有以下 SQL 查询,我需要让它在 HQL 中工作,我尝试了几个解决方案,但没有一个工作。(数据库:SQL 服务器)

select t.createdTimeStamp FROM ServiceData t , UserInfo C where C.UserId = t.UserId and  CAST(t.createdTimeStamp AS DATE) = '2016-05-30'

我尝试在 HQL 中进行以下操作,但没有成功,(createdTimeStamp 在 sql server 中属于 datetime 数据类型)

查询:

select t.createdTimeStamp FROM ServiceData t , UserInfo C where C.UserId = t.UserId and  CAST(t.createdTimeStamp AS DATE) = '2016-05-30'

错误:

org.hibernate.QueryException: Could not resolve requested type for CAST : DATE

任何有关如何使其工作的建议都会有所帮助。

【问题讨论】:

  • 你试过转换日期参数吗? select t.createdTimeStamp FROM ServiceData t , UserInfo C where C.UserId = t.UserId and CAST(t.createdTimeStamp AS DATE) = CAST('2016-05-30' as date)
  • @vercelli 我试过了,得到以下错误 " java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode [IDENT] IdentNode : 'createdTimeStamp' {originalText=createdTimeStamp} ] 根本原因 java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode "
  • 也许这会对你有所帮助:stackoverflow.com/questions/26471534/…
  • @vercelli,感谢您的建议,我在下面的 URL 中找到了帮助。StackOverflow

标签: java sql datetime hql


【解决方案1】:

你必须按照下面的方式编写cast命令,然后它就会起作用

cast(t.createdTimeStamp as date)

所以小写的“cast”、“as”和“date”

【讨论】:

【解决方案2】:

我从 StackOverflow URL 下面找到了答案 StackOverflow-date-is-not-a-recognized-built-in-function-name

我使用 CONVERT 而不是 CAST 并且它有效

查询:select t.createdTimeStamp FROM ServiceData t , UserInfo C where C.UserId = t.UserId and CONVERT(date,t.createdTimeStamp) = '2016-05-30'

但不确定为什么 CAST 不起作用,因为支持 Hibernate Doc CAST

【讨论】:

    猜你喜欢
    • 2013-09-16
    • 1970-01-01
    • 2010-09-05
    • 2013-06-28
    • 1970-01-01
    • 2013-04-08
    • 2016-09-19
    • 2018-07-29
    • 2018-05-22
    相关资源
    最近更新 更多