1

I have below query where i am getting error as

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.

I am using this query in stored procedure. The day_id column is number data type and the value stored in this column as YYYYMMDD. The month_id column is also number data type and i want to stored the value in this column as YYYYMM

INSERT INTO TEST_CHECK(MONTH_ID) VALUES
 (to_char(REC.day_id, 'YYYYMM'));
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Andrew
  • 3,632
  • 24
  • 64
  • 113

2 Answers2

3

You're applying a conversion to a number as if it was a date, trying to use the result (a string) as a number.

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

You may need something like:

SQL> INSERT INTO TEST_CHECK(MONTH_ID) VALUES(substr(to_char(20180101), 1, 6));

1 row created.

I would remember that storing dates and months in such a way is not a good idea.

Aleksej
  • 22,443
  • 5
  • 33
  • 38
1

Use

INSERT INTO TEST_CHECK(MONTH_ID) VALUES
 (substr(REC.day_id, 1, 6));

instead, since both day_id and month_id are numbers.

If day_id were in date format you could make such a conversion to char but this not the case.

You may apply to_char conversion for REC.day_id as to_char(REC.day_id) but if there's no non-numeric character, oracle considers number as char implicitly without to_char, during a string operation such as substr.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55