ORA-01830: 在 TOAD 中转换整个输入字符串之前日期格式图片结束

ORA-01830: date format picture ends before converting entire input string in TOAD

我有一个脚本,我正在其中编写函数和过程。该脚本在 Oracle SQL 开发人员中运行良好,没有任何错误或警告。这个脚本我必须提供给客户端客户。客户用户在 TOAD 中运行相同的脚本,他收到错误消息

ORA-01830: date format picture ends before converting entire input string

在这种情况下,我真的不知道如何处理这样的错误。这是 TOAD 中的错误吗? 我在我的函数中使用 to_dateto_timestamp,但出现错误。

您要么依赖隐式日期转换,要么依赖隐式格式模型;因为你说你正在使用 to_dateto_timestamp 它似乎是后者。您没有显示您的代码,但错误暗示您正在使用您希望看到的格式的字符串值调用这些函数,但没有明确提供格式模型作为函数的第二个参数。这将导致使用会话的 NLS_DATE_FORMAT 设置。如果你这样做:

to_date('12/06/2015 09:10:11')

那么你真的在做:

to_date('12/06/2015 09:10:11', (select value from nls_session_parameters where parameter = 'NLS_DATE_FORMAT'))

您无法控制您的客户将如何配置他们的 NLS 环境,因此您永远不应依赖隐式转换或 NLS 假设。

如其所说in the documentation:

Caution:
It is good practice always to specify a format mask (fmt) with TO_DATE, as shown in the examples in the section that follows. When it is used without a format mask, the function is valid only if char uses the same format as is determined by the NLS_TERRITORY or NLS_DATE_FORMAT parameters. Furthermore, the function may not be stable across databases unless the explicit format mask is specified to avoid dependencies.

您可以看到这对简单查询有何影响:

SQL> alter session set nls_date_format = 'DD/MM/YYYY HH24:MI:SS';

Session altered.

SQL> select to_date('12/06/2015 09:10:11') from dual;

TO_DATE('12/06/2015
-------------------
12/06/2015 09:10:11

SQL> alter session set nls_date_format = 'DD/MM/YYYY';

Session altered.

SQL> select to_date('12/06/2015 09:10:11') from dual;
select to_date('12/06/2015 09:10:11') from dual
               *
ERROR at line 1:
ORA-01830: date format picture ends before converting entire input string

出于同样的原因,您应该避免使用月份名称,因为它们的解释取决于 NLS_DATE_LANGUAGE;尽管如果确实需要,您至少可以在查询级别覆盖它。

如果您使用的是固定值,您可能还需要考虑日期和时间戳文字:

select date '2015-06-12' ...
select timestamp '2015-06-12 09:10:11' ...

但是如果您正在构造要转换为日期的字符串,那么这将不合适。