ORA-01481: invalid number format model in oracle insert query - sql

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'));

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.

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.

Related

data type in Oracle

how to resolve this?
INSERT INTO logiciel VALUES ('log1','Oracle 6',13-05-1995,'6.2','UNIX','3000');
INSERT INTO logiciel VALUES ('log1','Oracle 6',13-05-1995,'6.2','UNIX','3000')
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
the error
here's the table
the table
create table logiciel (
nLog varchar2 (5) primary key,
nomLog varchar (20) not null,
dateAchat date,
version varchar2 (7),
typeLog varchar2 (9),
prix number (6,2)
);
Use date literal or TO_DATE function with appropriate format mask. Do not insert strings into date datatype columns, hoping that Oracle will guess format you used.
SQL> -- date literal is always DATE 'yyyy-mm-dd'
SQL> INSERT INTO logiciel (nlog, nomlog, dateachat, version, typelog, prix)
2 VALUES ('log1', 'Oracle 6', date '1995-05-13', '6.2', 'UNIX', '3000');
1 row created.
SQL> -- TO_DATE lets you choose format, but then the format mask must follow it
SQL> INSERT INTO logiciel (nlog, nomlog, dateachat, version, typelog, prix)
2 VALUES ('log2', 'Oracle 6', to_date('13-05-1995', 'dd-mm-yyyy'), '6.2', 'UNIX', '3000');
1 row created.
SQL>
You have to use quotes around your date value as dates are treated as string -
INSERT INTO logiciel VALUES ('log1','Oracle 6','13-05-1995','6.2','UNIX','3000');
But only using string will tell the DB to store it as string not as date. The difference between them is, You can not do any date manipulation on strings. So you have 2 option now.
Use To_Date function with date format -
INSERT INTO logiciel VALUES ('log1','Oracle 6',TO_DATE('13-05-1995', 'DD-MM-YYYY'),'6.2','UNIX','3000');
Use DATE keyword which is supported by ANSI standard but with that, you have to use the date format as 'YYYY-MM-DD'-
INSERT INTO logiciel VALUES ('log1','Oracle 6',DATE '1995-05-13','6.2','UNIX','3000');

Why isn't Oracle converting characters to numbers?

Oracle throws ORA-01722: invalid number in my SQL query and it is unclear why.
I have a table called "LIGHTS" and I want to get the lights with a WATTAGE <= 3. WATTAGE is stored as a VARCHAR2(40) for some reason, but each character does seem to be an integer or float. When I convert WATTAGE to a number using the query:
SELECT TO_NUMBER(WATTAGE) FROM LIGHTS
There's no problem. I get a result like this:
TO_NUMBER(WATTAGE)
1
7
-1
0
15
17.5
However, when I add a WHERE condition to filter the numbers for those less than 3, I get the ORA-01722: invalid number error:
SELECT WATTAGE FROM LIGHTS
WHERE TO_NUMBER(WATTAGE) <= 3
What could be going wrong?
ORA-01722: invalid number comes from the TO_NUMBER(), not from the conditional. I.e., try this and you'll get the same error:
SELECT TO_NUMBER('test') FROM dual;
This would indicate that at least one of your values is not numeric.
Alas Oracle doesn't have a simple way to check whether a string is in fact representing a number. (One of the many reasons to use the correct data type in the first place!)
However, you can write your own. Here is just a brief demo of this concept. I create a table with a column of VARCHAR2 data type, and populate it with a few strings, one of which is not a number.
create table tbl (nbr varchar2(100));
insert into tbl
select '103' from dual union all
select '-1.3' from dual union all
select 'abc' from dual
;
Then I create a small function with a nested block that should error out if TO_NUMBER fails. The error handler will "do something" specific to errors and then return control to the main function. Then I can use this in a WHERE clause. Here are the function and then how it can be used to find the offending values:
create or replace function not_a_number(str varchar2)
return varchar2
as
x number;
r varchar2(100);
begin
begin
x := to_number(str);
exception
when others then
r := str;
end;
return r;
end;
/
select nbr
from tbl
where not_a_number(nbr) is not null;
NBR
-------
abc

SQL - literal does not match format string in INSERT INTO

I've created an empty table--in my website that holds a bunch of tables-- that has the following columns/data types:
NAME -- VARCHAR2
MRN -- NUMBER
DATE_S -- DATE
E -- DATE
DELI -- DATE
WB -- VARCHAR2
ST_ID -- VARCHAR2
COMMENTS --VARCHAR2
EI -- NUMBER
Below is one of almost 800 rows of code I am using to populate the table.
INSERT INTO SANDBOX.W_C VALUES ('S,E',11300033,'2012-02-18 00:00:00','2012-03-01 00:00:00','2013-02-18 00:00:00','N','006i',NULL,NULL);
When I run that piece of code I get the following message: literal does not match format string. What am I doing wrong?
You need to_Date
INSERT INTO SANDBOX.W_C VALUES ('S,E',11300033,
TO_DATE('2012-02-18', 'yyyy-mm-dd'),
TO_DATE('2012-03-01', 'yyyy-mm-dd'),
TO_DATE('2013-02-18', 'yyyy-mm-dd'),'N','006i',NULL,NULL);
When you provide a date as a string, the database uses it's default settings to try to convert the string. The best way to handle this is the use of to_date, as in scaisEdge's answer.
However, you can also change the default date mask using alter session before you run the insert statements:
ALTER SESSION SET NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss';

Wrong date format for input parameter?

CREATE OR REPLACE PROCEDURE PROC1(
V_STARTTIME IN DATE,
V_ENDTIME IN DATE)
BEGIN
INSERT INTO TAB1
SELECT COINS FROM TAB2
WHERE DATE BETWEEN TO_DATE(V_STARTTIME,'MM/DD/YYYY') AND TO_DATE(V_ENDTIME,'MM/DD/YYYY');
COMMIT;
END;
SAMPLE DATE in Tab2 IS TIMESTAMP DATATYPE 5/5/2014 9:46:38.000000 AM
When I try to execute
Execute PROC1(TO_DATE('5/5/2014','MM/DD/YYYY'),TO_DATE('5/6/2014','MM/DD/YYYY'));
the procedure is successfully completed but my Insert into was ignored.
I tried printing the input date through dbms_output.put_line and the date did not return.
This is very, very similar to the question you asked yesterday.
If v_starttime and v_endtime are of type date, it makes no sense to call to_date on them. to_date does not take an argument of type date. It takes a parameter of type varchar2. If you try to pass a date to to_date, Oracle has to implicitly cast the date to a varchar2 using the session's NLS_DATE_FORMAT. If that doesn't match the format mask you're passing to to_date, you may get an error or you may get an incorrect result. As in yesterday's question, you want to avoid implicit conversions.
A date in Oracle has both a day and a time component (to the second). If you are doing the to_date in order to ensure that the time component is midnight, use the trunc function instead.
INSERT INTO TAB1( column_name )
SELECT COINS
FROM TAB2
WHERE <<timestamp column>> BETWEEN trunc( v_starttime ) AND trunc( v_endtime );
You say that your "insert was ignored". That seems highly unlikely. It's much more likely that your SELECT statement returned 0 rows so your INSERT inserted 0 rows. That's not an error. If you want to treat it as an error, you'd need to check SQL%ROWCOUNT after the INSERT and throw an error if the INSERT statement inserts 0 rows.
If the SELECT was not selecting any rows because of an implicit conversion error, then getting rid of the to_date and potentially adding the trunc would fix the problem.
The function TO_DATE requires string as first parameter.
CREATE OR REPLACE PROCEDURE PROC1(
V_STARTTIME IN DATE,
V_ENDTIME IN DATE)
BEGIN
INSERT INTO TAB1
SELECT COINS FROM TAB2 WHERE DATE BETWEEN V_STARTTIME AND V_ENDTIME;
COMMIT; --You should not use commit in procedure.
END;

SQL query for date format for CLOB field is not working

Hi I am using the below query to get the date in the format 'DD-MM-YYYY' from a CLOB field from ORACLE Toad.
Select ID, NVL(TO_CHAR(xmltype(XML_RAW).extract('//ROWSET//ROW//MJR_V//MJR_V_ROW//EARLIEST_ACCEPT_DATE/text()').getStringVal(), 'DD-MM-YYYY'),'')
AS Dateformat from table1 where ID = 102
It thorws error:
ORA:01722: Invalid number
But if I use the above query direct DB columns (NOT CLOB FIELDS) then it executes fine.
Select ID, NVL(TO_CHAR(Start_Date, 'DD-MM-YYYY'),'')
AS Dateformat from table1 where ID = 102
Please let me the solution.
You shouldn't run to_char on varchar2
to_char function gets a date (or a number) as an argument and converts it to a varchar2.
If you use it on a varchar2, then oracle implicitly converts the string to a date according to NLS_DATE_FORMAT
So you sould probably do something like this: