ORA-01861: 文字與格式字串不匹配,ORA-01722: 無效數字
阿新 • • 發佈:2019-01-04
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這種格式。