ORA-01861: 文字与格式字符串不匹配,ORA-01722: 无效数字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Joyce_heart/article/details/78499626

SQL> create table Student(
  2         id varchar2(20) primary key,
  3         birth date
  4  )
  5  /

表已创建。

SQL> insert into Student values('1',to_date('2017/11/11','yyyy-mm-dd'));

已创建 1 行。

SQL> select birth from student
  2  /

BIRTH
--------------
11-11月-17

SQL> select to_date(birth,'yyyy-mm-dd') from student
  2  /
select to_date(birth,'yyyy-mm-dd') from student
               *
第 1 行出现错误:
ORA-01861: 文字与格式字符串不匹配


SQL> select to_char(birth,'yyyy-mm-dd') from student
  2  /

TO_CHAR(BIRTH,'YYYY-
--------------------
2017-11-11

SQL> alter table student modify birth varchar(20);
alter table student modify birth varchar(20)
                           *
第 1 行出现错误:
ORA-01439: 要更改数据类型, 则要修改的列必须为空


SQL> truncate table student;

表被截断。

SQL> select * from student;

未选定行

SQL> alter table student modify birth varchar(20);

表已更改。

SQL> insert into Student values('1','2017/11/11');

已创建 1 行。

SQL> select * from student;

ID
----------------------------------------
BIRTH
----------------------------------------
1
2017/11/11


SQL> select to_date(birth,'yyyy-mm-dd') from student;

TO_DATE(BIRTH,
--------------
11-11月-17

SQL> select to_char(birth,'yyyy-mm-dd') from student;
select to_char(birth,'yyyy-mm-dd') from student
               *
第 1 行出现错误:
ORA-01722: 无效数字


SQL> truncate table student;

表被截断。

SQL> alter table student modify birth date;

表已更改。

SQL> insert into Student values('1',to_date('2017/11/11','yyyy-mm-dd'));

已创建 1 行。

SQL> select birth from student
  2  /

BIRTH
--------------
11-11月-17

SQL> alter session set nls_date_format='YYYY-MM-DD';

会话已更改。

SQL> select birth from student;

BIRTH
----------
2017-11-11
总结:

1.当表的字段类型为date时,使用to_date()就会报ORA-01861: 文字与格式字符串不匹配;

2.当表的字段类型为varchar2时,使用to_char()就会报ORA-01722: 无效数字;

3.查看date类型的数据,发现格式为11-11月-17这种类型时,可以使用alter session set nls_date_format='YYYY-MM-DD';换成2017-11-11这种格式。

猜你喜欢

转载自blog.csdn.net/Joyce_heart/article/details/78499626