进度 SQL:将儒略日期转换为日期时间

时间:2021-05-11 15:26:09

标签: sql openedge progress-db

我为一个使用 PROGRESS 数据库 SQL 的客户工作(我不知道这种数据库,所以这是我第一次使用它)。 我的问题是所有日期都在 Julian-date 中,我想将它们转换为 datetime。但我还没有找到任何处理此问题的在线文档或帮助。

我找到的唯一文件是: https://knowledgebase.progress.com/articles/Article/How-to-Obtain-a-Julian-Date-in-Progress

但我想做恰恰相反的事情。 例如在 postgresql 中:

select to_timestamp(column1::text,'J')
from table1

但是在 PROGRESS 上它更难,而且信息和示例比网络上的其他数据库少

预先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

人物

如果您的儒略日期存储在具有字符数据类型的字段中,您可以使用 instr 将其拆开,然后将其重新组合成时间戳:

select 
  -- my character field containing 92182.3966
   descr,
   -- get the year
    floor( cast( left( descr, instr( descr, '.' ) - 1 ) as integer ) / 1000 ) as 'year',
    -- get the day
   mod( cast( left( descr, instr( descr, '.' ) - 1 ) as integer ), 1000 ) as 'day',
   -- get the time
   cast(
    '0' + right( descr, length( descr ) - instr( descr, '.' ) + 1 ) 
      as float
   ) as 'time',
   -- combine all to timestamp
   cast(
      -- get first day of year
      cast(       
        to_char( 
            floor( cast( left( descr, instr( descr, '.' ) - 1 ) as integer ) / 1000 ) 
            + 1900  -- !!! beware
        )
        + '-01-01' 
        as date
      ) 
      -- add days
      + mod( cast( left( descr, instr( descr, '.' ) - 1 ) as integer ), 1000 )

        as timestamp
     )  
     -- add milliseconds
     + cast(
        cast(
        '0' + right( descr, length( descr ) - instr( descr, '.' ) + 1 ) 
        as float
    ) * 86400 * 1000
    as integer
   ) as 'timestamp'  
   
from pub.ddcapp 
where application = 'JULIAN'

这会报告时间为 1992-07-01 09:31:06.24,这比您的链接声明的翻译来源晚了 1 秒。

十进制

另一方面,如果您的字段是小数,则简单得多:

select 
  -- my decimal field containing 92182.3966
   open_bal,
   -- get the year
    floor( open_bal / 1000 ) as 'year',
    -- get the day
   mod( open_bal, 1000 ) as 'day',
   -- get the time
    open_bal - floor( open_bal ) as 'time',
    
   -- combine all to timestamp
   cast(
      cast( 
        to_char( 
            floor( open_bal / 1000 )
            + 1900  -- !!!
        )
        + '-01-01' 
        as date
      ) 
      + mod( open_bal, 1000 )    
        as timestamp        
    ) 
    + ( open_bal - floor( open_bal ) ) * 86400 * 1000
     as 'timestamp'
   
from pub.ledbal 
where adm_nr = 0
相关问题