Oracle: year must be between -4713 and +9999, and not be 0

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

I have an Oracle table like this

|---------------------------------| |EMPNO |    HIREDATE  | INDEX_NUM |   |---------------------------------|   |1     |   2012-11-13 | 1         | |2     |   2          | 1         | |3     |   2012-11-17 | 1         | |4     |   2012-11-21 | 1         | |5     |   2012-11-24 | 1         | |6     |   2013-11-27 | 1         | |7     |   2          | 2         | |---------------------------------| 

I am trying to execute this query against this table

SELECT hiredate   FROM admin_emp   WHERE TO_DATE('hiredate','yyyy-mm-dd') >= TO_DATE('2012-05-12','yyyy-mm-dd'); 

But getting the error

ORA-01841: (full) year must be between -4713 and +9999, and not be 0 

Any idea..? What is the issue here?

query base:

CREATE TABLE admin_emp (          empno      NUMBER(5) PRIMARY KEY,          hiredate   VARCHAR(255),          index_num NUMBER(5));   insert into admin_emp(empno,hiredate,index_num) values (1,'2012-11-13',1); insert into admin_emp(empno,hiredate,index_num) values (2,'2',1); insert into admin_emp(empno,hiredate,index_num) values (3,'2012-11-17',1); insert into admin_emp(empno,hiredate,index_num) values (4,'2012-11-21',1); insert into admin_emp(empno,hiredate,index_num) values (5,'2012-11-24',1); insert into admin_emp(empno,hiredate,index_num) values (6,'2013-11-27',1); insert into admin_emp(empno,hiredate,index_num) values (7,'2',2); 

回答1:

Single quotes (') in SQL denote string literals. So 'hiredate' isn't the hiredate column, it's just a varchar, which, of course, doesn't fit the date format you're specifying. Just drop the quotes and you should be fine:

SELECT hiredate FROM   admin_emp WHERE  TO_DATE(hiredate,'yyyy-mm-dd') >= -- No quotes         TO_DATE('2012-05-12','yyyy-mm-dd'); 


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