ORA-01858 Error Message

泄露秘密 提交于 2019-12-12 03:23:22

问题


I have a question regarding date format error. So i have this query

SELECT TO_DATE (PS.PROJECT_START_DT, 'MM/DD/YYYY')

and gives me ORA-01858: a non-numeric character was found where a numeric was expected

my table structure is this

PROJECT_START_DT PROJECT_END_DT JOB_NAME
2/5/2015         4/2/2015       W-IGG
2/18/2015        3/19/2015      W14056
2/5/2015         3/31/2015      W14013

Please help me with my issue with date format


回答1:


SELECT TO_DATE (PS.PROJECT_START_DT, 'MM/DD/YYYY')

It makes no sense to use TO_DATE over a DATE column. You need TO_CHAR to display in the desired format using a proper FORMAT MODEL.

SQL> SELECT to_date(SYSDATE, 'mm/dd/yyyy') FROM dual;
SELECT to_date(SYSDATE, 'mm/dd/yyyy') FROM dual
               *
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected


SQL>
SQL> SELECT to_char(SYSDATE, 'mm/dd/yyyy') FROM dual;

TO_CHAR(SY
----------
02/06/2015

SQL>

The DATE datatype has both date and time elements,

SQL> SELECT to_char(SYSDATE, 'mm/dd/yyyy hh24:mi:ss') dt_tm FROM dual;

DT_TM
-------------------
02/06/2015 11:28:32

SQL>

If you want to use the DATE value in any date operation, then you don't have to convert it into anything at all, simply use the DATE value directly for date operations.

Update On OP's request about the query here http://pastie.org/9891465

In that query, you don't have to use TO_CHAR in the sub-query. Use TO_CHAR only in the main select since that is what will display the values. The start_date and end_dt in the sub-query is an INPUT to the outer query and should remain as DATE values and need not convert it into a string.

SELECT LEVEL WEEK_NUM_INCR,
  TO_CHAR (start_date + (LEVEL - 1) * 7, 'WW') WEEK_POSITION
  /* WEEK POSITION FOR THE WHOLE YEAR */
  ,
  TO_CHAR (start_date + (LEVEL - 1) * 7, 'MM/DD/YYYY') START_WEEK_DATE,
  TO_CHAR (start_date + (LEVEL) * 7, 'MM/DD/YYYY') END_WEEK_DATE
FROM
  (SELECT PS.PROJECT_START_DT start_date,
    PS.PROJECT_END_DT end_date
  FROM PROJECT_SPAN PS
  WHERE PS.JOB_NAME = 'W-IGG'
  )
  CONNECT BY start_date + (LEVEL - 1) * 7 < end_date;


来源:https://stackoverflow.com/questions/28359371/ora-01858-error-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!