有什么方法可以将VARCHAR2转换为SECONDS吗?

时间:2017-04-17 02:25:24

标签: sql oracle plsql

我的输出就像这样

**Column1**              **Column2** 
20170123012057.555  20170123070616.314

如果我减去两列

,我应该得到20719秒

3 个答案:

答案 0 :(得分:0)

您可以将值转换为日期,然后采用差异:

select (to_date(substr(column2, 1, 14), 'YYYYMMDDHH24MISS') -
        to_date(substr(column1, 1, 14), 'YYYYMMDDHH24MISS')
       ) * 24 * 60 * 60

答案 1 :(得分:0)

试试这个。在这里,我给出了milliseconds以及seconds中的舍入

的区别
select 
extract(day from (to_timestamp(column2,'YYYYMMDDHH24MISS.FF')
                 -to_timestamp(column1,'YYYYMMDDHH24MISS.FF')
                 )*86400*1000) / 1000 
    as diff_in_milliseconds  
,round(extract(day from (to_timestamp(column2,'YYYYMMDDHH24MISS.FF')-
                         to_timestamp(column1,'YYYYMMDDHH24MISS.FF')
                         )*86400*1000) / 1000
     ) as diff_in_seconds
from 
(select
'20170123012057.555' as Column1,
'20170123070616.314' as Column2
 from dual)

输出

DIFF_IN_MILLISECONDS    DIFF_IN_SECONDS
20718,7590              20719

答案 2 :(得分:0)

尝试

SELECT ROUND (seconds / 1000) seconds
  FROM (SELECT   EXTRACT (DAY FROM cal_col) * 24 * 60 * 60 * 1000
               + EXTRACT (HOUR FROM cal_col) * 60 * 60 * 1000
               + EXTRACT (MINUTE FROM cal_col) * 60 * 1000
               + ROUND (EXTRACT (SECOND FROM cal_col) * 1000)
                  seconds
          FROM (SELECT   TO_TIMESTAMP ('20170123070616.314',
                                       'YYYYMMDDHH24MISS.FF')
                       - TO_TIMESTAMP ('20170123012057.555',
                                       'YYYYMMDDHH24MISS.FF')
                          cal_col
                  FROM DUAL))