---
title: "oracle基础日期查询"
author: "xihuan&java"
date: "2011-09-29T12:28:02Z"
tags: ["Oracle"]
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2011-11-04T11:18:37Z"
id: 1183074
url: "https://wdwheyr.iteye.com/blog/1183074"
---

开发中遇到了oracle的时间查询记录下来以便以后查阅
hibernate查询指定日期的数据：

```java
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代码（没经过测试）

```java

--查询当天的数据
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()：
```java
to_date('"+xxx+"', 'YYYY-MM-DD HH24:MI:ss') --得到的是一个时间格式
```

to_char：
```java
to_char(t.locodate,'yyyy-mm')='2009-12' --得到的是字符串格式
```