java 将字符串列转换为 hql 中的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28106999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Casting string column to date in hql
提问by Vimal Basdeo
I need to get all records of date superior than 01/01/2015. This query does not work since nt.valeur is of dataType varchar(255). I cannot change the structure of the entity NotifTypeActionChampAddValVO . The database server is on a db2 version 9.7.
我需要获取日期早于 01/01/2015 的所有记录。此查询不起作用,因为 nt.valeur 是 dataType varchar(255)。我无法更改实体 NotifTypeActionChampAddValVO 的结构。数据库服务器在 db2 9.7 版上。
Here is the hql:
这是 hql:
select nt.valeur from NotifTypeActionChampAddValVO nt where nt.valide = 1 and nt.typeActionChampAdditionnelValue.champAdditionnel.id = 123 and cast(nt.valeur as date) = '01/01/2015')
I tried by casting cast(nt.valeur as date) but it generates an exception.
我尝试通过强制转换 cast(nt.valeur as date) 但它产生了一个异常。
here is the exception
这是例外
DIAVERUM 2015-01-23 13:23:21,859 [http-8081-7] ERROR org.hibernate.util.JDBCExceptionReporter - DB2 SQL Error: SQLCODE=-461, SQLSTATE=42846, SQLERRMC=SYSIBM.LONG VARCHAR;SYSIBM.DATE, DRIVER=3.63.75
DIAVERUM 2015-01-23 13:23:21,859 [http-8081-7] ERROR org.hibernate.util.JDBCExceptionReporter - DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-461;42846;SYSIBM.LONG VARCHAR|SYSIBM.DATE, DRIVER=3.63.75
How can I cast the column What are the ways this string column can be compared as a date in an hql?
我如何转换列 这个字符串列可以作为 hql 中的日期进行比较的方式是什么?
回答by ikettu
You can use db2 to_date function to cast different string formats to date. For example
您可以使用 db2 to_date 函数将不同的字符串格式转换为日期。例如
date(to_date(column,'DD-MM-YYYY HH:MI:SS'))
So you query could be (depends on the format used on the column nt.valeur
)
所以你查询可能是(取决于列上使用的格式nt.valeur
)
select nt.valeur from NotifTypeActionChampAddValVO nt where nt.valide = 1 and nt.typeActionChampAdditionnelValue.champAdditionnel.id = 123 and date(to_date(nt.valeur,'DD-MM-YYYY HH:MI:SS')) = '01/01/2015')
请参阅:TO_DATE 标量函数
to_date is actually alias for TIMESTAMP_FORMATfunction where format of the format string is documented better.
to_date 实际上是TIMESTAMP_FORMAT函数的别名,其中格式字符串的格式记录得更好。
回答by Predrag Maric
You can do this without trying to cast string to date. Try this
您可以在不尝试将字符串转换为日期的情况下执行此操作。试试这个
select nt.valeur from NotifTypeActionChampAddValVO nt where nt.valide = 1 and nt.typeActionChampAdditionnelValue.champAdditionnel.id = 123 and concat(substring(nt.valeur, 7, 4), substring(nt.valeur, 4, 2), substring(nt.valeur, 1, 2)) > '20150101'
This is under assumption that 01/01/2015
from your example is dd/MM/yyyy
(but can be changed easily), and that you can adapt the parameter format to yyyyMMdd
.
这是假设01/01/2015
您的示例是dd/MM/yyyy
(但可以轻松更改),并且您可以将参数格式调整为yyyyMMdd
.
回答by Mahender Yadav
Try this one:-
试试这个:-
from NotifTypeActionChampAddValVO nt where nt.valide = 1 and nt.typeActionChampAdditionnelValue.champAdditionnel.id = 123 and nt.valeur ='2015-01-01'