将十进制年份转换为日期

Convert decimal year to date

我在 table 中有 stored as decimal years 的日期。一个例子是 2003.024658 转换为 January 9, 2003我想将十进制年份转换为 Oracle 的日期格式。


我在 Excel 中找到了这样做的人:Decimal year to date formula?

 =DATE(INT(B1),1,MOD(B1,1)*(DATE(INT(B1)+1,1,1)-DATE(INT(B1),1,1)))

但是,我不太明白如何将逻辑转换为 Oracle PL/SQL。

如果您假设小数部分是根据给定年份天数计算的(即 365 或 366,具体取决于是否为闰年),你可以这样做:

with
 q1 as (select 2003.024658 d from dual)
,q2 as (select d
              ,mod(d,1) as decimal_portion
              ,to_date(to_char(d,'0000')||'0101','YYYYMMDD')
               as jan01
        from q1)
,q3 as (select q2.*
              ,add_months(jan01,12)-jan01 as days_in_year
        from q2)
select d
      ,decimal_portion * days_in_year as days
      ,jan01 + (decimal_portion * days_in_year) as result
from   q3;

d: 2003.024658
days: 9.00017
result: 10-JAN-2003 12:00am