To_Char 中的日期格式出错

Getting Error with Date Format in To_Char

我在第二个脚本中遇到错误。请解释为什么我在第二个脚本中出错。

select count(*) from LCL_SHR_IncidentIntegrationInt where externalsystem = 'IPSOFT' and (to_char(sysdate,'YYYYMMDDHH24MISS')-to_char(fn_adjusted_date(CREATE_DATE),'YYYYMMDDHH24MISS')) > 180;

O/P : 122797

select count(*) from LCL_SHR_IncidentIntegrationInt where externalsystem = 'IPSOFT' and (to_char(sysdate,'DD-MM-YYYY HH24:MI:SS')-to_char(fn_adjusted_date(CREATE_DATE),'DD-MM-YYYY HH24:MI:SS')) > 180;

O/P : ORA-01722: invalid number 01722. 00000 - "invalid number" *Cause: The specified number was invalid. *Action: Specify a valid number.

第一个查询有效,因为 Oracle 能够将字符隐式转换为数字并与 180 进行比较。另一个查询不行,因为 : 和 - 不能隐式转换为数字。 对于 timestampdiffdatediff 这样的情况,您应该使用日期和时间函数。更多功能在这里 - https://docs.oracle.com/cd/E17952_01/refman-5.1-en/date-and-time-functions.html