---
title: "Oracle中的时间查询和分页"
author: "ww1224"
date: "2010-12-29T11:11:29Z"
tags: ["Oracle", "SQL"]
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2010-12-29T11:12:42Z"
id: 854663
url: "https://ww1224.iteye.com/blog/854663"
---

根据时间查询一般我是用to_char()方法,我知道这样肯定不是好方法。但是其他的我还不太会
所以我都说这样写的：
  
```sql
select * from (     
  select row_.* , rownum row_num from (      
  select * from T_AN_UNIQUE_NEW        
  where to_char(NEWS_DATE,'yyyy-mm-dd hh24:mi:ss')>='2010-12-22 00:00:01' 
  and to_char(NEWS_DATE,'yyyy-mm-dd hh24:mi:ss')<='2010-12-29 23:59:59' 
 ) row_   where rownum <=30 ) where row_num >=0 
```

希望有朋友能指点下更好的方法。
比如说、数据库中是date类型、在程序中却是String类型的时间字符串或者程序中也是date类型
怎么查询才更加方便。还有就是timestamp类型时程序中是String类型或者也是date类型。怎么处理才更加方便效率更高呢？

因为在Oracle中不支持top写法、也不支持limit
所以在分页时一般是给定起始值和结束值、查询其中的数据。
例如：

```sql
select * from (
     select row_.* , rownum row_num from (
    select t.*   from t_channel t   
  )  row_ where rownum <= 30) where row_num >0
```

row_num：起始行
rownum ：结束行