试图在Oracle中以分钟为单位计算格式为(00-00-0000 00:00:00)的两个日期之间的差异

问题描述 投票:0回答:3

我在表t_mst中具有以下格式a_time=01-06-2020 15:28:06b_time=01-06-2020 15:39:00的日期我想在几分钟之内将给定值的差异转换为查询

select (extract( day from diff )*24*60*60 +
         extract( hour from diff )*60*60 +
         extract( minute from diff )*60 +
         extract( second from diff ))/60  total_Minutes
    from(select b_time - a_time diff
           from t_mst Where a_time between (trunc(sysdate, 'mm')) and (sysdate))

但出现错误“提取源无效的提取字段”任何想法将不胜感激。

oracle plsql
3个回答
1
投票

您显示的错误表示b_time - a_time有效。这意味着它们是date(或也许是timestamp)数据类型;很好。

然后,它们必须为date;如果它们是timestamp,则差异将是interval数据类型,并且您确实可以从interval中提取时间元素。因此,错误消息表明a_timeb_time的数据类型的确是date

现在:在Oracle中,两个日期之间的差为number(以天为单位);您无法从数字中提取时间元素。相反,请考虑到差异已经是几天(包括小数部分)了;要将其转换为分钟,只需要将其乘以1440-然后根据需要舍入结果。


0
投票

您已标记了3个数据库,并且代码适用于postgreSQL。我想你的意思是,postgreSQL:

select (extract( epoch from age(a_time, b_time)/60)  total_Minutes
    from t_mst 
--Where a_time between (trunc(sysdate, 'mm')) and (sysdate)) 

如果您需要将其强制转换为int:

select (extract( epoch from age(a_time, b_time)/60)::int  total_Minutes
    from t_mst 

PS:不确定您的意思是plSQL或PL \ pgSQL。


0
投票

如果b_timea_time数据类型为DATE,则只需运行

select (b_time - a_time) * 24*60 AS total_Minutes
from t_mst 
Where a_time between trunc(sysdate, 'mm') and sysdate

[如果它们是TIMESTAMP数据类型,则必须提取值,例如像这样:

select 
    EXTRACT(DAY FROM (b_time - a_time))*24*60
    + EXTRACT(HOUR FROM (b_time - a_time))*60
    + EXTRACT(MINUTE FROM (b_time - a_time))
    + EXTRACT(SECOND FROM (b_time - a_time))/60
    AS total_Minutes
from t_mst 
Where a_time between trunc(sysdate, 'mm') and sysdate
© www.soinside.com 2019 - 2024. All rights reserved.