Sql ORA-01481:oracle插入查询中的数字格式模型无效
我在下面的问题中得到了错误Sql ORA-01481:oracle插入查询中的数字格式模型无效,sql,oracle,date,insert,ora-01481,Sql,Oracle,Date,Insert,Ora 01481,我在下面的问题中得到了错误 ORA-01481: invalid number format model The user is attempting to either convert a number to a string via TO_CHAR or a string to a number via TO_NUMBER and has supplied an invalid number format model parameter. 我正在存储过程中使用此查询。day_id列是数字数
ORA-01481: invalid number format model
The user is attempting to either convert a number to a string via TO_CHAR or a string
to a number via TO_NUMBER and has supplied an invalid number format model parameter.
我正在存储过程中使用此查询。day_id列是数字数据类型,该列中存储的值为YYYYMMDD
。month_id列也是数字数据类型,我想将该列中的值存储为YYYYMM
INSERT INTO TEST_CHECK(MONTH_ID) VALUES
(to_char(REC.day_id, 'YYYYMM'));
您正在将转换应用于一个数字,就像它是一个日期一样,试图将结果(字符串)用作一个数字
SQL> create table TEST_CHECK(MONTH_ID number);
Table created.
SQL> INSERT INTO TEST_CHECK(MONTH_ID) VALUES(to_char(20180101, 'YYYYMM'));
INSERT INTO TEST_CHECK(MONTH_ID) VALUES(to_char(20180101, 'YYYYMM'))
*
ERROR at line 1:
ORA-01481: invalid number format model
您可能需要以下内容:
SQL> INSERT INTO TEST_CHECK(MONTH_ID) VALUES(substr(to_char(20180101), 1, 6));
1 row created.
我会记得,以这种方式存储日期和月份不是一个好主意。您将数字转换为日期,试图使用结果(字符串)作为数字
SQL> create table TEST_CHECK(MONTH_ID number);
Table created.
SQL> INSERT INTO TEST_CHECK(MONTH_ID) VALUES(to_char(20180101, 'YYYYMM'));
INSERT INTO TEST_CHECK(MONTH_ID) VALUES(to_char(20180101, 'YYYYMM'))
*
ERROR at line 1:
ORA-01481: invalid number format model
您可能需要以下内容:
SQL> INSERT INTO TEST_CHECK(MONTH_ID) VALUES(substr(to_char(20180101), 1, 6));
1 row created.
我会记得,以这种方式存储日期和月份不是一个好主意。使用
INSERT INTO TEST_CHECK(MONTH_ID) VALUES
(substr(REC.day_id, 1, 6));
相反,因为day\u id
和month\u id
都是数字
如果day_id为date
格式,则可以将其转换为char,但情况并非如此
您可以将REC.day\u id
的字符转换为到\u char(REC.day\u id)
,但如果没有非数字字符
,oracle会在substr
等字符串操作期间,将数字
视为字符
而不使用字符
INSERT INTO TEST_CHECK(MONTH_ID) VALUES
(substr(REC.day_id, 1, 6));
相反,因为day\u id
和month\u id
都是数字
如果day_id为date
格式,则可以将其转换为char,但情况并非如此
您可以将
REC.day\u id
的字符转换为to\u char(REC.day\u id)
,但如果没有非数字字符
,oracle会在substr
to\u char()等字符串操作期间将number
视为char
而不隐式转换为\u char
返回一个VARCHAR
而不是一个数字,因此您不能将其存储到数字列中如果您只想丢失最后两位数字,那不是trunc(day\u id/100)
?到\u char()
返回一个VARCHAR
而不是一个数字,因此您无法将其存储到数字列中如果您只想丢失最后两位数字,那不是trunc(day\u id/100)
?