一个查询的问题??

c099633 2012-04-12 11:24:37
查询1
select *
from a
where to_date('2012-3-31 13:00:01',
'yyyy-mm-dd hh24:mi:ss') between start_date and end_date

如果用这句查询就不能查询出数据
查询2
select *
from a
where to_date('2012-3-31',
'yyyy-mm-dd hh24:mi:ss') between start_date and end_date
如果去掉时分秒,就能查询出数据。
这是为什么呢?
start_date 和end_date字段都是DATE类型

我自己做了个测试

declare
v_date date;
begin
select to_date('2011-3-31 13:00:01', 'yyyy-mm-dd hh24:mi:ss')
into v_date
from dual;
if v_date > to_date('2011-3-31', 'yyyy-mm-dd') then
dbms_output.put_line('1');
else
dbms_output.put_line('2');
end if;
exception
when others then
dbms_output.put_line('3');
end;

最后返回了 1 ,说明带时分秒的是可以和不带时分秒的比较的啊。。
我有点迷茫了。。
那位大神告诉我下原因。。

...全文
61 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssqtjffcu 2012-04-12
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
查询1
select *
from a
where to_date('2012-3-31 13:00:01',
'yyyy-mm-dd hh24:mi:ss') between start_date and end_date

如果用这句查询就不能查询出数据
查询2
select *
from a
where to_date('2012-3-31',
'y……
[/Quote]

没有指定时分秒的话相当于时分秒是0例如to_date('2012-3-31',
'yyyy-mm-dd hh24:mi:ss')等同于to_date('2012-3-31 00:00:00',
'yyyy-mm-dd hh24:mi:ss')
没_没_没超疯 2012-04-12
  • 打赏
  • 举报
回复
to_date()之后,转换成的类型都是一样的,只不过带时分秒的精度比不带时分秒的精度要高些,也就是说不带时分秒的转换后默认成“20120-04-12 00:00:00”这个时刻。
你的查询没数据出来,应该是表字段里对时间进行了截取,enddate只到'2011-3-31 00:00:00'吧

17,378

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧