日期:2014-05-17  浏览次数:20687 次

一个查询的问题??
查询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 ,说明带时分秒的是可以和不带时分秒的比较的啊。。
我有点迷茫了。。
那位大神告诉我下原因。。



------解决方案--------------------
to_date()之后,转换成的类型都是一样的,只不过带时分秒的精度比不带时分秒的精度要高些,也就是说不带时分秒的转换后默认成“20120-04-12 00:00:00”这个时刻。
你的查询没数据出来,应该是表字段里对时间进行了截取,enddate只到'2011-3-31 00:00:00'吧
------解决方案--------------------
探讨
查询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……