0

我有一个这样的查询:

INSERT INTO some_table (date1, date2) 
VALUES (to_date('2012-10-24','YYYY-MM-DD'), to_date('2012-10-24','YYYY-MM-DD'));

但我得到:

SQL Error: ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 -  "a non-numeric character was found where a numeric was expected"

和:

*Cause:    The input data to be converted using a date format model was
           incorrect.  The input data did not contain a number where a number was
           required by the format model.
*Action:   Fix the input data or the date format model to make sure the
           elements match in number and type.  Then retry the operation.

但我的用法to_date似乎还可以?

非常感谢。

4

1 回答 1

4

这看起来是正确的。它对我有用,所以问题必须有其他不属于你的例子的东西......

SQL> create table some_table( date1 date, date2 date );

Table created.

SQL> INSERT INTO some_table (date1, date2)
  2  VALUES (to_date('2012-10-24','YYYY-MM-DD'), to_date('2012-10-24','YYYY-MM-DD'));

1 row created.

你真的在使用硬编码的文字吗?还是您要传递的字符串to_date来自桌子?表中是否有可能(至少)有一行数据与预期格式不匹配。请注意,最终被WHERE子句过滤掉的行,即使是WHERE子查询中的子句,仍然可以to_date在被过滤掉之前调用并导致错误。所以你有类似的东西是完全合理的

INSERT INTO some_table( date1, date2 )
  SELECT to_date( string1, 'YYYY-MM-DD' ), to_date( string2, 'YYYY-MM-DD' )
    FROM (SELECT *
            FROM some_other_table
           WHERE condition_that_limits_the_data_to_just_valid_date_strings )
   WHERE some_other_condition

返回错误。

于 2012-10-24T21:21:22.757 回答