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

oracle时间转换
我有一个时间格式:20020320000000,现在用to_date('20020320000000','yyyy-mm-dd hh24:mi:ss')转换后变成了
2002-03-20,如果用2010-10-10 10:10:10来比较大小的时候,会报ORA-01861: 文字与格式字符串不匹配

这种问题请问怎么处理?

------解决方案--------------------
2010-10-10 10:10:10 这个是什么类型,这个也要是DATE类型
to_date('2010-10-10 10:10:10','yyyy-mm-dd hh24:mi:ss')
------解决方案--------------------
处理成一样的格式看看
SELECT To_Char(To_Date('20020320000000'),'yyyy-mm-dd hh24:mi:ss') FROM dual;

SELECT To_Char(To_Date('2010-10-10 10:10:10'),'yyyy-mm-dd hh24:mi:ss') FROM dual;
------解决方案--------------------
你把语句写出来
时间和时间是可以比较的,


select * from 
(select to_date('20020320000000','yyyy-mm-dd hh24:mi:ss') a from dual) where a < to_date('2010-10-10 10:10:10','yyyy-mm-dd hh24:mi:ss')

------解决方案--------------------
SQL code
SELECT 'x' FROM dual 
WHERE to_date('20020320000000','yyyy-mm-dd hh24:mi:ss')>=to_date('2010-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and 
to_date('20020320000000','yyyy-mm-dd hh24:mi:ss')<=to_date('2010-01-10 00:00:00','yyyy-mm-dd hh24:mi:ss')

------解决方案--------------------
begin
if to_date('20020320000000','yyyy-mm-dd hh24:mi:ss')>to_date('2010-10-10 10:10:10','yyyy-mm-dd hh24:mi:ss')
then dbms_output.put_line('ok');
elsif to_date('20020320000000','yyyy-mm-dd hh24:mi:ss')<=to_date('2010-10-10 10:10:10','yyyy-mm-dd hh24:mi:ss')
then dbms_output.put_line('error');
end if;
end;

同类型才能比较
------解决方案--------------------
SQL code

--同类型才能比较
begin
  if to_date('20020320000000','yyyy-mm-dd hh24:mi:ss')>to_date('2010-10-10 10:10:10','yyyy-mm-dd hh24:mi:ss')
    then dbms_output.put_line('ok');
  elsif to_date('20020320000000','yyyy-mm-dd hh24:mi:ss')<=to_date('2010-10-10 10:10:10','yyyy-mm-dd hh24:mi:ss')
    then dbms_output.put_line('error');
  end if;
end;

------解决方案--------------------
类型必须要一样
------解决方案--------------------
to_date('2010-10-10 10:10:10','yyyy-mm-dd hh24:mi:ss')和to_date('20020320000000','yyyy-mm-dd hh24:mi:ss')才能比较。'2010-10-10 10:10:10'和to_date('20020320000000','yyyy-mm-dd hh24:mi:ss')一个varchar一个date不能比较
------解决方案--------------------
时间转换前 需要判断下长度 格式 不然转换不了报错