Oracle sql: ORA-01830 日期计算发生时

Oracle sql: ORA-01830 when date calculation happens

我想执行以下 oracle SQL 查询

SELECT t1.Technology, count(t1.trax_id) as "Current number of items", to_char(to_date(max(round((SYSDATE - t1.time_event) * 24 * 60 * 60)),'sssss'),'hh24:mi:ss') as "max_ages"
from dm_procmon t1
group by t1.Technology;

问题出在日期减法公式上。

I susbtract 2 dates from each other. This gives me a decimal value (like 0,00855605). I want the value back to be a date value. So I converted this first to a Number (Decimal > Number) and than to a char (Number > Char) Finally from a char to date (Char > Date).

但是当我执行我收到的操作时 错误报告-

SQL Error: ORA-01830: Datumnotatieafbeelding eindigt voordat de gehele invoerstring is geconverteerd. 01830. 00000 - "date format picture ends before converting entire input string"

我做错了什么?

您尝试转换 to_date(%number of seconds%, 'sssss'),这就是问题所在。只需使用 TO_CHAR(MAX(TO_DATE('20000101','yyyymmdd')+(SYSDATE - t1.time_event)),'hh24:mi:ss');这将在 < 1day

的间隔内正常运行

这是一个对最大间隔大小没有任何限制的通用解决方案:

select Technology, cnt as "Current number of items", FLOOR(x) || ':' || TO_CHAR(TO_DATE('20000101','yyyymmdd')+(x - FLOOR(x)),'hh24:mi:ss') as "max_ages"
from
    (select t1.Technology, COUNT(t1.trax_id) cnt, MAX(SYSDATE - t1.time_event) x
    from dm_procmon t1
    group by t1.Technology);

FLOOR(x) returns天和其余的(x - FLOOR(x))被添加到一个常数日期并转换为小时,分钟和秒

以下将给出一个 DAY TO SECOND INTERVAL 所有日期相差大约 68 年内的日期:

SELECT t1.technology, COUNT(t1.trax_id) AS "Current number of items"
     , NUMTODSINTERVAL(ROUND((SYSDATE - MIN(t1.time_event) * 24 * 60 * 60), 'SECOND') AS "max_ages"
  FROM dm_procmon t1
 GROUP BY t1.technology;

请注意,我使用 SYSDATE-MIN(time) 而不是使用 MAX(SYSDATE - time)(逻辑上相同)。使用它而不是 TO_CHAR() 的优点是您可以使用 DATE/INTERVAL 算术中返回的值。