DB2 SQL Updating a table value - sql

I'm trying to update a table value that is initially set to 0, I'm working with DB2. However, when I go to execute my SQL I get the following error:
DSNT408I SQLCODE = -406, ERROR: A CALCULATED OR DERIVED NUMERIC VALUE IS NOT
WITHIN THE RANGE OF ITS OBJECT COLUMN
DSNT418I SQLSTATE = 22003 SQLSTATE RETURN CODE
I understand what the error means, but I do not understand why I am getting it. Here is my SQL:
UPDATE INTTABLE
SET PAYMENT = DECIMAL((MONTHIRATE*OMA)/(1-POWER(1+MONTHIRATE,-420)),8,2);
Where PAYMENT is defined as DECIMAL(8,2)
Could someone please explain to me why the above UPDATE statement will not work?

Probably what is happening is that the calculation you are doing is somewhere getting a result with more than 6 digits before the decimal place.
DB2 will handle having more numbers after the decimal place than you have defined in the SCALE, but it will error (with the -406 you are seeing) when there are more digits than allowed with the PRECISION defined.
Just as an aside, do make sure you realize that a DECIMAL(8,2) will give you 6 places before the decimal and 2 after.
Edit: I think this query will show you the offending row(s):
SELECT * FROM (
SELECT
A.*
,(MONTHIRATE*OMA)/(1-POWER(1+MONTHIRATE,-420)) AS CALC
FROM INTTABLE A
) B
WHERE CALC > 999999

Related

SQL counter flag is not filtered

I'm using SQL developer and I'm getting an error I don't fully understand.
Due to data privacy, I cannot share the real query, but the question is fairly simple.
I have a query to obtain table T (which works if ran alone) and I want to filter a Y/N flag so I have the following query:
select * from T where flag ='Y'
While this query works, I want the observations which do not fullfil the condition but neither of the following queries works:
select * from T where flag <>'Y'
select * from T where flag ='N'
*I get the following error:
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
Action: Specify a valid number .
Vendor code 1722
I tried to export the table to analyze it on another language, but I still get the same error.
Can anyone help?
As you confirmed T in your example is not a table, your error simply seems to be caused by a conversion error somewhere on 1 or several records WHERE flag <> 'Y'. This is why the query works with flag = 'Y' but not with your other conditions.
Minimal case to reproduce would be something like:
CREATE TABLE MyTable (
Field VARCHAR(10),
Flag VARCHAR(1)
);
CREATE VIEW T AS SELECT to_number(Field), Flag FROM MyTable;
INSERT INTO MyTable VALUES ('1', 'Y'); /* OK */
INSERT INTO MyTable VALUES ('No way', 'N'); /* Fails */
In your case, the error may not be caused by a call to to_number but this is the best clue I can give given the limited information you provide. It might not be a call to a function at all but the result of a calculation using operators only.
You simply need to make sure the fields can be converted/used in a calculation before you actually attempt to do so (CASE WHEN ... THEN ... ELSE NULL for instance).

Getting "attribute or element value is larger than specified type" error for no reason - SQL (Oracle)

For some reason I'm getting an attribute larger than specified in type for some of my insert statement despite that not being the case and equivalent statements working correctly!
Can anyone help?
I tired to insert 10 rows but 4 failed.
Insert Statement
Table Structure
(mobileNumsArray has a max length of 12 - VARCHAR2(12) )

Update statement failing without any rows

On a SQL Server 2005 database a stored procedure that had been working starting failing. The following statement in the stored procedure was determined to be the cause of the failure:
update d set
d.location=a.[Name]+'-'+cast(a.lLocaId as varchar)
--select d.logical_name,d.location,a.[Name]+'-'+cast(a.lLocaId as varchar) as location
from hpsmp.dbo.device2m1 d
inner join hpsmp.dbo.locm1 s
on d.location=s.location
inner join hpamp.dbo.amLocation a
on s.location_code=a.lLocaId
where isnumeric(s.location_code)=1
and s.location_name<>a.[Name];
The error message is
Msg 8152, Level 16, State 2, Line 1
String or binary data would be truncated.
The statement has been terminated.
What is odd about this error is that the select statement is not returning any rows. Why would there be a truncation error in an update statement without any rows? There are no triggers on this table.
Your question is really: "What is odd about this error is that the select statement is not returning any rows. Why would there be a truncation error in an update statement without any rows? "
First, when you do the conversion to varchar you should always include a length:
set d.location = a.[Name] + '-' + cast(a.lLocaId as varchar(255))
SQL Server has different default lengths in different contexts, so this could cause a problem. Not the one you are seeing but another one.
I believe that what is happening is that SQL Server constructs the execution plan for the query and it moves the calculation of the new value of location before the filtering. In other words, it can do the calculation while reading hpamp.dbo.amLocation because that is the only table needed for the new value.
Then, it gets an error even on a row that is not being updated.
This is a bit of speculation, but SQL Server does exhibit this behavior in other places. A notorious problem is getting an error when you do:
select cast(col as datetime)
from table t
where isdate(col) = 1;
Yes, this also produces errors on invalid dates and for the same reason.
In your case, I'm not sure what the best way to fix the problem is. You could try something like:
set d.location = (case when len(a.name) < 8 then a.[Name] + '-' + cast(a.lLocaId as varchar(255)) end)
I made up the number 8, but if you set an appropriate value, then it should work.

SQL error "ORA-01722: invalid number"

A very easy one for someone,
The following insert is giving me the
ORA-01722: invalid number
why?
INSERT INTO CUSTOMER VALUES (1,'MALADY','Claire','27 Smith St Caulfield','0419 853 694');
INSERT INTO CUSTOMER VALUES (2,'GIBSON','Jake','27 Smith St Caulfield','0415 713 598');
INSERT INTO CUSTOMER VALUES (3,'LUU','Barry','5 Jones St Malvern','0413 591 341');
INSERT INTO CUSTOMER VALUES (4,'JONES','Michael','7 Smith St Caulfield','0419 853 694');
INSERT INTO CUSTOMER VALUES (5,'MALADY','Betty','27 Smith St Knox','0418 418 347');
An ORA-01722 error occurs when an attempt is made to convert a character string into a number, and the string cannot be converted into a number.
Without seeing your table definition, it looks like you're trying to convert the numeric sequence at the end of your values list to a number, and the spaces that delimit it are throwing this error. But based on the information you've given us, it could be happening on any field (other than the first one).
Suppose tel_number is defined as NUMBER - then the blank spaces in this provided value cannot be converted into a number:
create table telephone_number (tel_number number);
insert into telephone_number values ('0419 853 694');
The above gives you a
ORA-01722: invalid number
Here's one way to solve it. Remove non-numeric characters then cast it as a number.
cast(regexp_replace('0419 853 694', '[^0-9]+', '') as number)
Well it also can be :
SELECT t.col1, t.col2, ('test' + t.col3) as test_col3
FROM table t;
where for concatenation in oracle is used the operator || not +.
In this case you get : ORA-01722: invalid number ...
This is because:
You executed an SQL statement that tried to convert a string to a
number, but it was unsuccessful.
As explained in:
Oracle/PLSQL: ORA-01722 Error.
To resolve this error:
Only numeric fields or character fields that contain numeric values
can be used in arithmetic operations. Make sure that all expressions
evaluate to numbers.
As this error comes when you are trying to insert non-numeric value into a numeric column in db it seems that your last field might be numeric and you are trying to send it as a string in database. check your last value.
Oracle does automatic String2number conversion, for String column values! However, for the textual comparisons in SQL, the input must be delimited as a String explicitly: The opposite conversion number2String is not performed automatically, not on the SQL-query level.
I had this query:
select max(acc_num) from ACCOUNTS where acc_num between 1001000 and 1001999;
That one presented a problem: Error: ORA-01722: invalid number
I have just surrounded the "numerical" values, to make them 'Strings', just making them explicitly delimited:
select max(acc_num) from ACCOUNTS where acc_num between '1001000' and '1001999';
...and voilĂ : It returns the expected result.
edit:
And indeed: the col acc_num in my table is defined as String. Although not numerical, the invalid number was reported. And the explicit delimiting of the string-numbers resolved the problem.
On the other hand, Oracle can treat Strings as numbers. So the numerical operations/functions can be applied on the Strings, and these queries work:
select max(string_column) from TABLE;
select string_column from TABLE where string_column between '2' and 'z';
select string_column from TABLE where string_column > '1';
select string_column from TABLE where string_column <= 'b';
In my case the conversion error was in functional based index, that I had created for the table.
The data being inserted was OK. It took me a while to figure out that the actual error came from the buggy index.
Would be nice, if Oracle could have gave more precise error message in this case.
If you do an insert into...select * from...statement, it's easy to get the 'Invalid Number' error as well.
Let's say you have a table called FUND_ACCOUNT that has two columns:
AID_YEAR char(4)
OFFICE_ID char(5)
And let's say that you want to modify the OFFICE_ID to be numeric, but that there are existing rows in the table, and even worse, some of those rows have an OFFICE_ID value of ' ' (blank). In Oracle, you can't modify the datatype of a column if the table has data, and it requires a little trickery to convert a ' ' to a 0. So here's how to do it:
Create a duplicate table: CREATE TABLE FUND_ACCOUNT2 AS SELECT * FROM FUND_ACCOUNT;
Delete all the rows from the original table: DELETE FROM FUND_ACCOUNT;
Once there's no data in the original table, alter the data type of its OFFICE_ID column: ALTER TABLE FUND_ACCOUNT MODIFY (OFFICE_ID number);
But then here's the tricky part. Because some rows contain blank OFFICE_ID values, if you do a simple INSERT INTO FUND_ACCOUNT SELECT * FROM FUND_ACCOUNT2, you'll get the "ORA-01722 Invalid Number" error. In order to convert the ' ' (blank) OFFICE_IDs into 0's, your insert statement will have to look like this:
INSERT INTO FUND_ACCOUNT (AID_YEAR, OFFICE_ID) SELECT AID_YEAR, decode(OFFICE_ID,' ',0,OFFICE_ID) FROM FUND_ACCOUNT2;
I have found that the order of your SQL statement parameters is also important and the order they are instantiated in your code, this worked in my case when using "Oracle Data Provider for .NET, Managed Driver".
var sql = "INSERT INTO table (param1, param2) VALUES (:param1, :param2)";
...
cmd.Parameters.Add(new OracleParameter("param2", Convert.ToInt32("100")));
cmd.Parameters.Add(new OracleParameter("param1", "alpha")); // This should be instantiated above param1.
Param1 was alpha and param2 was numeric, hence the "ORA-01722: invalid number" error message. Although the names clearly shows which parameter it is in the instantiation, the order is important. Make sure you instantiate in the order the SQL is defined.
For me this error was a bit complicated issue.
I was passing a collection of numbers (type t_numbers is table of number index by pls_integer;) to a stored procedure. In the stored proc there was a bug where numbers in this collection were compared to a varchar column
select ... where ... (exists (select null from table (i_coll) ic where ic.column_value = varchar_column))
Oracle should see that ic.column_value is integer so shouldn't be compared directly to varchar but it didn't (or there is trust for conversion routines).
Further complication is that the stored proc has debugging output, but this error came up before sp was executed (no debug output at all).
Furthermore, collections [<empty>] and [0] didn't give the error, but for example [1] errored out.
The ORA-01722 error is pretty straightforward. According to Tom Kyte:
We've attempted to either explicity or implicity convert a character string to a number and it is failing.
However, where the problem is is often not apparent at first. This page helped me to troubleshoot, find, and fix my problem. Hint: look for places where you are explicitly or implicitly converting a string to a number. (I had NVL(number_field, 'string') in my code.)
This happened to me too, but the problem was actually different: file encoding.
The file was correct, but the file encoding was wrong. It was generated by the export utility of SQL Server and I saved it as Unicode.
The file itself looked good in the text editor, but when I opened the *.bad file that the SQL*loader generated with the rejected lines, I saw it had bad characters between every original character. Then I though about the encoding.
I opened the original file with Notepad++ and converted it to ANSI, and everything loaded properly.
In my case it was an end of line problem, I fixed it with dos2unix command.
In my case I was trying to Execute below query, which caused the above error ( Note : cus_id is a NUMBER type column)
select *
from customer a
where a.cus_id IN ('115,116')
As a solution to the caused error, below code fragment(regex) can be used which is added in side IN clause (This is not memory consuming as well)
select *
from customer a
where a.cus_id IN (select regexp_substr (
com_value,
'[^,]+',
1,
level
) value
from (SELECT '115,116' com_value
FROM dual)rws
connect by level <=
length ( com_value ) - length ( replace ( com_value, ',' ) ) + 1)
try this as well, when you have a invalid number error
In this
a.emplid is number and b.emplid is an varchar2 so if you got to convert one of the sides
where to_char(a.emplid)=b.emplid
You can always use TO_NUMBER() function in order to remove this error.This can be included as INSERT INTO employees phone_number values(TO_NUMBER('0419 853 694');

Conversion error on host variable or parameter *N

I am getting this Error on Insert statement to AS400 database, using Java with JDBC.
I figured it out, I had a numeric field with length 4 and decimal digits 2, In my insert I was trying to insert 100 in this numeric field which gave this error.
I altered the table and made it numeric with length 9 and decimal digits 2 and the insert worked.
even though the error says "Conversion error", actually its the wrong field length.
I ran in to this issue when a table was updated from 2 digits to 3 for user ID's and the query inserts in to a history table where the history table was not updated to 3 digits for the user id. Used Alter Table to correct the history table and all is well.
alter table "Table Name" ALTER COLUMN "COLUMN NAME" SET DATA TYPE NUMERIC(3)
The error message is not intuitive:
Conversion error on variable or parameter *N (#-302) [IBM][System
iAccess ODBC Driver][DB2 for i5/OS]SQL0012 - Correlation without
qualification occurred for column "columnname" to table "Tablename".
(#12)
I got this error today running the following query:
select *
from mytable
where invoice_date >= '2019-01-01'
Turned out invoice "date" is not a date... it's a decimal that impersonates a date. Poor database design, perhaps, but nonetheless the simple fix was:
select *
from mytable
where invoice_date >= 20190101
My issue was I overlooked the fact that I had single quotes around my parameter place holder. So I was getting a, 6 -- Numeric data that is not valid. error.
So, for example:
select someColumn
From favTable
where someOtherColumn = '?'
command.Parameters.Add("decVal", OleDbType.Decimal, 10).Value = model.someDecVal;
Correcting to:
select someColumn
From favTable
where someOtherColumn = ?
solved my issue.
Looks easy to catch here, but with big queries, this can be easily overlooked. Wasted about an hour on this.