2012-10-24 57 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