`
tianhandigeng
  • 浏览: 368943 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Oracle 时间段查询

阅读更多

    需求:根据用户输入的时间段查询出记录。

Oracle数据库中一个一个存放时间的字段,字段类型是DATE型的,其中有这样的两条数据



 这个字段存有时分秒,用户输入的时候是按日期来查询的,也就是说只有年月日,最初我是这样查询的:

select * from tb_product where createdate>=to_date('2011-6-13','yyyy-MM-dd') and createdate<=to_date('2011-6-16','yyyy-MM-dd');

 这样查询的话2011/6/16这条记录是查不出来的,因为字段存了时分秒。

最后通过群里的朋友的指点改成了这样:

select * from tb_product where to_char(createdate,'yyyy-MM-dd')>='2011-6-13' and to_char(createdate,'yyyy-MM-dd')<='2011-6-16';

 这样也查不出来,不是语句错了,而是我日期格式错了,日期格式是‘yyyy-MM-dd’这样的,而我写的是2011-6-13,改成

2011-06-13和2011-06-16就可以了。

   上面的方法就是只按照日期查询的方法了,各位有更好的方法,请留言。

 

 

  • 大小: 4.2 KB
分享到:
评论
34 楼 lylovejava0 2013-01-25  
数据库里字段不能转换。。这样会慢死。。在说如果表里有分区等设置那你的分区就不会起作用 反而走了反作用。。
赞同select * from tb_product where trunc(createdate)>=? and trunc(createdate)<=? 这个方式
33 楼 liujianche11 2011-06-21  
小心点  createdate<=to_date('2011-6-16','yyyy-MM-dd')是查不出6月16号 带时分秒的数据的 最好写全
32 楼 iceside 2011-06-21  
1.建函数索引
2.用第一种方法,注意to_date的范围应该是 00:00
31 楼 zhangjie.0211 2011-06-20  
字符串, 直接>或者<
30 楼 tiannet 2011-06-20  
hzl7652 写道
楼主可以将起始时间转化成
'年-月-日 00:00:00'
终止时间转化成
'年-月-日 23:59:59'
再传给oracle,这样sql也不需要进行什么加1的运算了


常用这种方法。
29 楼 红蚂蚁 2011-06-20  
wxwdt 写道
select * from tb_product where trunc(createdate)>=? and trunc(createdate)<=?
用trunc函数就可以了

只能说能实现
28 楼 红蚂蚁 2011-06-20  
nirvana1988 写道
fengyexjtu 写道
wad12302 写道
select * from tb_product where

createdate > to_date('2011-6-15','yyyy-MM-dd') - 1

and

createdate<=to_date('2011-6-16','yyyy-MM-dd') + 1;


个人认为这种方式好一些

+1

最标准的,别的都比较傻。
27 楼 openFox 2011-06-20  
nakupanda 写道
yongqi 写道
我不明白
select * from tb_product where createdate>=to_date('2011-6-13','yyyy-MM-dd') and createdate<=to_date('2011-6-17','yyyy-MM-dd'); 


这样的写法2011/6/16这条记录为什么查不出来,我一直是这样用的,没发现这个问题?谁讲一下?



不明白TOO



2011/6/16没记录....
26 楼 chansman 2011-06-20  
richard_2010 写道
wxwdt 写道
select * from tb_product where trunc(createdate)>=? and trunc(createdate)<=?
用trunc函数就可以了



+1


select * from tb_product where createdate>=to_date('2011-06-13','yyyy-MM-dd') and createdate<=to_date('2011-06-16','yyyy-MM-dd'); 
注意临界时间2011-06-16指得是2011-06-16 00:00:00
25 楼 chansman 2011-06-20  
tianhandigeng 写道
chansman 写道
你还是应该使用第一种方法,因为第二种方法吧数据库中所有的数据都做了 to_char.
to_date 是 yyyy-mm-dd 不是MM 同时你还应该注意临界时间

把数据库中所有的数据都做了 to_char. 这怎么里面,是数据库中所有的记录都这样处理了?


不要对数据库中的子段做操作.
24 楼 richard_2010 2011-06-19  
wxwdt 写道
select * from tb_product where trunc(createdate)>=? and trunc(createdate)<=?
用trunc函数就可以了



+1
23 楼 tianhandigeng 2011-06-19  
yongqi 写道
我不明白
select * from tb_product where createdate>=to_date('2011-6-13','yyyy-MM-dd') and createdate<=to_date('2011-6-17','yyyy-MM-dd'); 


这样的写法2011/6/16这条记录为什么查不出来,我一直是这样用的,没发现这个问题?谁讲一下?

写错了 是2011-6-16
22 楼 yongqi 2011-06-18  
tfwin2 写道
yongqi 写道
我不明白
select * from tb_product where createdate>=to_date('2011-6-13','yyyy-MM-dd') and createdate<=to_date('2011-6-17','yyyy-MM-dd'); 


这样的写法2011/6/16这条记录为什么查不出来,我一直是这样用的,没发现这个问题?谁讲一下?




这么用是没问题的,估计楼主的代码或语句有问题,oracle的时间段,是可以这样查的,
经严格测试,至少9I,10G两个版本,这么查没有问题,2011/6/16是可以查出来得,依据楼主对数据库知识的匮乏度,只能推测,代码出错。。。。


我用的是10g,确实没遇到过这样的问题。
21 楼 redish 2011-06-18  
select * from tb_product where createdate>=to_date('2011-06-13','yyyy-MM-dd') and createdate<=to_date('2011-06-17','yyyy-MM-dd');  
这样可以查询出来,楼主怎么就查不出来?

另外数据库时间格式也可用字符串格式varchar2(14)  (YYYYMMDDHH24MISS) ,因为不涉及到国外的时间,这样使用很简便。
20 楼 tfwin2 2011-06-18  
在时间戳这种字段一般都会带有索引,任何函数转换 如TO_CHAR,TRUNC,都会使索引失效,查询速度影响巨大,除非冒更大的风险将索引建在函数基础上。
ORACLE中日期是以7位长的数值储存的,如果我没记错,应该是针对1970年的一个偏移量,
故2011/6/17的偏移量,一定大于2011/6/16 23:59:59秒
所以查不出,不可能是数据库设计问题,只能是使用问题。
仔细想想也知道,如果日期查询还要用字符串做比较,那oracle花这么大劲设计一套跟其他数据库都不同的日期处理机制做什么?
19 楼 tfwin2 2011-06-18  
yongqi 写道
我不明白
select * from tb_product where createdate>=to_date('2011-6-13','yyyy-MM-dd') and createdate<=to_date('2011-6-17','yyyy-MM-dd'); 


这样的写法2011/6/16这条记录为什么查不出来,我一直是这样用的,没发现这个问题?谁讲一下?




这么用是没问题的,估计楼主的代码或语句有问题,oracle的时间段,是可以这样查的,
经严格测试,至少9I,10G两个版本,这么查没有问题,2011/6/16是可以查出来得,依据楼主对数据库知识的匮乏度,只能推测,代码出错。。。。
18 楼 hzl7652 2011-06-18  
楼主可以将起始时间转化成
'年-月-日 00:00:00'
终止时间转化成
'年-月-日 23:59:59'
再传给oracle,这样sql也不需要进行什么加1的运算了
17 楼 wad12302 2011-06-18  
wad12302 写道
select * from tb_product where

createdate > to_date('2011-6-15','yyyy-MM-dd') - 1

and

createdate<=to_date('2011-6-16','yyyy-MM-dd') + 1;


上面写有问题:

好像是应该是

>=  aaa

and

<  bbb - 1



已修改为这种方式
16 楼 nakupanda 2011-06-18  
yongqi 写道
我不明白
select * from tb_product where createdate>=to_date('2011-6-13','yyyy-MM-dd') and createdate<=to_date('2011-6-17','yyyy-MM-dd'); 


这样的写法2011/6/16这条记录为什么查不出来,我一直是这样用的,没发现这个问题?谁讲一下?



不明白TOO
15 楼 way 2011-06-18  
wad12302 写道
select * from tb_product where

createdate > to_date('2011-6-15','yyyy-MM-dd') - 1

and

createdate<=to_date('2011-6-16','yyyy-MM-dd') + 1;

不需要-1吧,上面那种就是从6月15号0时0分0秒开始查询的,-1就从14号开始查了

相关推荐

Global site tag (gtag.js) - Google Analytics