当某些日期格式具有 DDYYYY 而某些日期格式在 oracle 中具有 DDDYYYY 时,将一年中的某一天转换为日期 [英] Converting Day of the Year to a date when some date formats have DDYYYY and some have DDDYYYY in oracle

查看:64
本文介绍了当某些日期格式具有 DDYYYY 而某些日期格式在 oracle 中具有 DDDYYYY 时,将一年中的某一天转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图将一年中的某一天日期转换为日期格式.问题是一年中的某天日期在 100 天以下的天数采用 DDYYYY 格式,100 天及以上的天数采用 DDDYYYY 格式.我尝试了以下操作,但仍然收到一年中的某一天必须在 1 到 365 之间(闰年为 366)"错误:

So I am trying to convert some Day of the Year dates to Date format. The problem is some day of the year dates are in DDYYYY format for days under 100 and DDDYYYY format for days 100 and over. I have tried the following but still receive a "day of year must be between 1 and 365 (366 for leap year)" error:

select CASE when data_Date >= 999999
        then to_date(data_date, 'DDDYYYY')
        when data_Date >= 99999
        then to_date(data_Date, 'DDYYYY')
        else to_date(data_date, 'DYYYY')
    END                 as         DATA_DATE_CONVERTED
 from table;

提前致谢

样本数据如下:

Data_date (being passed in as a varchar2)

1072015
12017
612014
672013
72017
1112018

推荐答案

最后 4 位数字是年份.使用 LPAD 将前导零放在 DAY

The last 4 digit is the year. Use LPAD to put leading zeroes on DAY

select  to_date( lpad(dayyear, 7, '0'),'DDDYYYY')  
from table;

sqlfiddle

这篇关于当某些日期格式具有 DDYYYY 而某些日期格式在 oracle 中具有 DDDYYYY 时,将一年中的某一天转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆