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

이것이 작동해야합니다. [demo] (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) 프로그램에서 가져옵니다. DB에 다른 (날짜가 아닌) 필드가 있습니다. 실험을 보겠습니다. – ale

관련 문제