我在下面的sql中收到错误:
ORA-01858: a non-numeric character was found where a numeric was expected
SELECT c.contract_num,CASE WHEN ( MAX (TO_CHAR (TO_DATE (c.event_dt,'YYYY-MM-DD'),'MMDD')) - MIN (TO_CHAR (TO_DATE (c.event_dt,'MMDD'))) / COUNT (c.event_occurrence) < 32 THEN 'Monthly' WHEN ( MAX ( TO_CHAR (TO_DATE (c.event_dt,'MMDD')) - MIN ( TO_CHAR (TO_DATE (c.event_dt,'MMDD'))) / COUNT (c.event_occurrence) >= 32 AND ( MAX ( TO_CHAR (TO_DATE (c.event_dt,'MMDD'))) / COUNT (c.event_occurrence) < 91 THEN 'Quarterley' ELSE 'Yearly' END FROM ps_ca_bp_events c GROUP BY c.contract_num;
您得到的错误要么是因为您在已经是日期的列上执行TO_DATE,并且您使用的格式掩码与您的nls_date_format参数[1]不同,或者因为event_occurrence列包含的数据不是数.
您需要a)更正您的查询,以便它不在日期列上使用TO_DATE,并且b)更正您的数据,如果event_occurrence应该只是数字.
并修复该列的数据类型以确保您只能存储数字.
[1] Oracle执行的操作:TO_DATE(date_column,non_default_format_mask)是:
TO_DATE(TO_CHAR(date_column,nls_date_format),non_default_format_mask)
通常,默认的nls_date_format参数设置为dd-MON-yy,因此在您的查询中,可能发生的事情是您的日期列被转换为dd-MON-yy格式的字符串,然后您将转向它使用MMDD格式返回日期.字符串不是这种格式,因此您会收到错误.