开发中遇到了oracle的时间查询记录下来以便以后查阅
hibernate查询指定日期的数据:
查询当天,当周,当月的sql代码(没经过测试)
日期查询to_date():
to_char:
hibernate查询指定日期的数据:
String hql = "select bean from ZJczbJiPoTongJi bean where 1=1 ";
if(bean!=null && bean.getRiqi() !=null){
hql += "and to_char(bean.riqi,'yyyy-mm-dd') = "+"'"+dateFormat.format(bean.getRiqi())+"')";
}
查询当天,当周,当月的sql代码(没经过测试)
--查询当天的数据
select * from a where generateTime=sysdate
--查询一个星期的数据
select * from a where (sysdate-generaeTime)=7
--查询一个月的数据
select * from a where months_between(sysdate,generateTime)=1
--查询一年的数据
1.select * from table t
2.where t.create_time >=trunc(sysdate,'YYYY')
3.and t.create_time<=add_months(trunc(sysdate,'YYYY'),12)-1
日期查询to_date():
to_date('"+xxx+"', 'YYYY-MM-DD HH24:MI:ss') --得到的是一个时间格式
to_char:
to_char(t.locodate,'yyyy-mm')='2009-12' --得到的是字符串格式