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

请教个日期补足0问题
比如:
2012112,应该是20120112,
2012812,应该是20120812,
2012112,应该是20121102,
有没有什么方法直接select出后面的结果的,我to_date的时候就出错了,01861,01830

------解决方案--------------------
你这个有歧义 啊,2012112,到底是20120112还是20121102,
是要哪一个,还是2个都要啊


------解决方案--------------------
select to_date('2012112', 'yyyymmdd') from dual ---- 肯定是 2012/11/2
------解决方案--------------------
SQL code

WITH t  AS
(SELECT '201101' num FROM dual
UNION ALL
SELECT '20111' num  FROM dual
UNION ALL
SELECT '201111' num FROM dual) 
SELECT CASE WHEN length(num) = 6 THEN num
                       WHEN  length(num) = 5 THEN substr(num,1,4)||'0'||substr(num,5,1)
                       ELSE '日期错误' END
                       FROM t

------解决方案--------------------
针对第二个那种没有歧义的数据,这样应该可以
SQL code

select to_date(decode(substr(datestr,5,1),'1',datestr,substr(datestr,1,4)||'0'||substr(datestr,5)),'yyyymmdd') from tablename;