2012-10-24 58 views
0

我有这样的查询:在此Oracle查询中使用to_date有什么问题?

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使用似乎还好吗?

非常感谢。

+2

这应该是工作,看到这个[演示](http://sqlfiddle.com/#!4/e75a8/1)。这是您尝试运行的唯一查询吗? 'date1'和'date2'的数据类型是什么? – Taryn

回答

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 

会返回错误。

+0

+1感谢贾斯汀的提示。这些值来自(PHP)程序。我在数据库中有其他(非日期)字段..我会看看一个实验。 – ale