ORA-01722: invalid number when a specific number 112 is used - sql

WITH
TEST_RESULT_CTE AS
(SELECT R.DSDW_RESULT_ID,
R.PARAM_VALUE AS TEST_ID
FROM SLIMS_POC_RESULT_DETAIL R
JOIN SLIMS_POC_PARAMETER P ON P.PARAM_ID=R.PARAM_ID
WHERE P.PARAMETER_NAME='TEST_ID' AND P.CATEGORY = 'Result' )
SELECT * FROM
(
SELECT S.SAMPLE_ID, R.DSDW_RESULT_ID, PARA.PARAMETER_NAME as PNAME, R.PARAM_VALUE as PVALUE
FROM SLIMS_POC_RESULT_DETAIL R
JOIN TEST_RESULT_CTE TR ON TR.DSDW_RESULT_ID = R.DSDW_RESULT_ID
JOIN SLIMS_POC_TEST T ON T.TEST_ID = TR.TEST_ID
JOIN SLIMS_POC_SAMPLE S ON S.SAMPLE_ID = T.SAMPLE_ID --AND S.SAMPLE_ID = to_char(113)
JOIN SLIMS_POC_PARAMETER PARA ON PARA.PARAM_ID=R.PARAM_ID AND PARA.CATEGORY='Result'
)
Result_Data
PIVOT
(
MAX(PVALUE) FOR PNAME IN ( 'TEST_ID', 'RESULT_NAME', 'UNIT', 'RESULT_TEXT', 'VALUE', 'STATUS', 'ENTERED_ON', 'ENTERED_BY', 'RESULT_TYPE' )
) PIVOTED_TAB
WHERE SAMPLE_ID > 111
ORDER BY SAMPLE_ID;
The above sql Query provides an output, without any error.
However if I replace '111' with '112' in WHERE cluase, I get the following error:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
This error is quite strange to me, and that's why tough to fix.

The error is most likely from the data that is being returned from your new sample_id. There is a string being converted to a number that is failing. Check your data for invalid numerical data in varchar columns. Note that it could be an implicit conversion, not necessarily one where you are doing a to_number() call.

Related

ORA-22992 / ORA-06502+ORA-06512 errors

I'm using this part of code in large one :
SELECT DISTINCT
P.SKU, SUBSTR(X.ATTRIBUTENAME, 14, 3) ATTRIBUTECODE, X.ATTRIBUTEVALUE
FROM
PRODUCT#ISPSTAG2 P,
XMLTABLE('/attrs/attr'
PASSING XMLTYPE(REGEXP_REPLACE(P.ATTRIBUTES_DE_AT, '<attr name="longDescription">.*?<\/attr>'))
COLUMNS ATTRIBUTENAME VARCHAR2(50) PATH '#name',
ATTRIBUTEVALUE VARCHAR2(4000) PATH '/string'
) X
WHERE X.ATTRIBUTENAME LIKE 'Z_CA%'
AND DN(DOMAINID) = 'AT'
AND SKU NOT LIKE 'OFF_%' AND SKU NOT LIKE 'PDT%'
AND ATTRIBUTES_DE_AT IS NOT NULL;
And it throws an ORA-22992 error.
I've made some research and a tip can to use the dual
SELECT DISTINCT P.SKU, SUBSTR(X.ATTRIBUTENAME, 14, 3) ATTRIBUTECODE, X.ATTRIBUTEVALUE
FROM PRODUCT#ISPSTAG2 P,
XMLTABLE('/attrs/attr'
PASSING XMLTYPE(REGEXP_REPLACE(P.ATTRIBUTES_DE_AT, '<attr name="longDescription">.*?<\/attr>'))
COLUMNS ATTRIBUTENAME VARCHAR2(50) PATH '#name',
ATTRIBUTEVALUE VARCHAR2(4000) PATH '/string'
) X
WHERE X.ATTRIBUTENAME LIKE 'Z_CA%'
AND DN(DOMAINID) = 'AT'
AND SKU NOT LIKE 'OFF_%' AND SKU NOT LIKE 'PDT%'
AND (SELECT ATTRIBUTES_DE_AT FROM DUAL) IS NOT NULL;
But now I get ORA-06502/ORA-06512 errors :
ORA-06502: PL/SQL : erreur numérique ou erreur sur une valeur
ORA-06512: à "SYS.XMLTYPE", ligne 272
ORA-06512: à ligne 1
06502. 00000 - "PL/SQL: numeric or value error%s"
*Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2).
*Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.
But, I executed the first one on ISPSTAG2 and it works, but the second one on ISPSTAG2 returns me the same ORA-06502/ORA-06512 errors so the issue is with the dual subquery.
I also tried to create a view on ISPSTAG2 using DBMS_LOB.SUBSTR with 4000 characters but same error.
Any ideas ? Thank you.
If forgot to use the (SELECT ATTRIBUTES_DE_AT FROM DUAL) subquery inside the XMLTYPE...
SELECT DISTINCT P.SKU, SUBSTR(X.ATTRIBUTENAME, 14, 3) ATTRIBUTECODE, X.ATTRIBUTEVALUE
FROM PRODUCT#ISPSTAG2 P,
XMLTABLE('/attrs/attr'
PASSING XMLTYPE(REGEXP_REPLACE(**(SELECT ATTRIBUTES_DE_AT FROM DUAL)**, '<attr name="longDescription">.*?<\/attr>'))
COLUMNS ATTRIBUTENAME VARCHAR2(50) PATH '#name',
ATTRIBUTEVALUE VARCHAR2(4000) PATH '/string'
) X
WHERE X.ATTRIBUTENAME LIKE 'Z_CA%'
AND DN(DOMAINID) = 'AT'
AND SKU NOT LIKE 'OFF_%' AND SKU NOT LIKE 'PDT%'
AND (SELECT ATTRIBUTES_DE_AT FROM DUAL) IS NOT NULL;
The thing I don't understand is that when don't use the subquery in the IS NOT NULL filter I have the the ORA-22992 error (using distant LOB), so why I have a different error not using the dual subquery, which is the same distant LOB ?
Anyway for you for your time/help :)

Invalid length parameter passed to the LEFT or SUBSTRING function (need hand)

I am getting following error when running this sql script
Invalid length parameter passed to the LEFT or SUBSTRING function
my sql query:
select distinct a.IMPORTBTCHSTATID, b.IMPORTBATCHID,
SUBSTRING(a.XMLREQBODY, charindex('<PersonNumber>',a.XMLREQBODY)+14, (charindex('</PersonNumber>',a.XMLREQBODY)-charindex('<PersonNumber>',a.XMLREQBODY)-14)) as PersonNum
from FAILEDXMLBODY a inner join
IMPORTBTCHSTAT b
on a.IMPORTBTCHSTATID=b.IMPORTBTCHSTATID
where b.IMPORTBATCHID in ('252','253','265')
This happens because CHARINDEX() returns 0 if the string is not found and this generates an error. A simple solution is to return NULL if a value is not found:
SUBSTRING(a.XMLREQBODY,
NULLIF(charindex('<PersonNumber>', a.XMLREQBODY), 0) + 14,
NULLIF(charindex('</PersonNumber>', a.XMLREQBODY), 0) - NULLIF(charindex('<PersonNumber>', a.XMLREQBODY), 0) - 14)
) as PersonNum

weird query behavior oracle sql - convert text to number

oracle query This works
select *
from (
select to_number(substr(app_cluster,6,2), '99') as b
from xtern_app_info
WHERE app_cluster IS NOT NULL
AND APP_CLUSTER <> 'CLUSTER'
);
but when adding 'where b > 2' makes an error, why?
select *
from (
select to_number(substr(app_cluster,6,2), '99') as b
from xtern_app_info
WHERE app_cluster IS NOT NULL
AND APP_CLUSTER <> 'CLUSTER'
) where b > 2;
ORA-29913: error in executing ODCIEXTTABLEFETCH callout
ORA-01722: invalid number
29913. 00000 - "error in executing %s callout"
*Cause: The execution of the specified callout caused an error.
*Action: Examine the error messages take appropriate action.

No Column name was specified for column 3 of 'eu'

Trying to set up this sql as a data connection in excel 2007:
select sv.accountid, sv.companyName, sv.siteid, sv.siteName + '('+sv.siteCity + '-' + sv.siteState + ')' as siteNameWithLocation,
datefromparts(datepart(yyyy,r.logdate),datepart(mm,r.logdate),1) as monthDate,
sum('controllerEstKGal') / 1000 as siteEstimatedTotalKgals
from RuntimeDay r WITH (READUNCOMMITTED)
join controller c on c.id = r.controllerid
join siteView sv on sv.siteid = c.siteid
join (
SELECT r.controllerid, r.logdate, SUM((ISNULL(r.RuntimeSec, 0)/60.0) * ISNULL(s.EsrfGpm, 0)) as 'controllerEstKGal'
FROM RuntimeDay r WITH (READUNCOMMITTED)
JOIN CdcView c WITH (READUNCOMMITTED) ON r.ControllerId = c.ControllerId
JOIN StationFlowConfig s WITH (READUNCOMMITTED)
ON r.ControllerId = s.ControllerId AND r.StationNumber = s.StationId
WHERE c.siteid in (8547, 8299, 8556, 8541, 8292, 8600, 8551, 5487, 8555, 8216, 8342, 8557, 8287, 8542, 8221, 5509, 8218, 8543, 8336, 8343)
AND logDate between '2016-01-01' and '2016-12-31'
AND r.RuntimeSec > 0
AND s.EsrfGpm > 0
group by r.controllerid, r.logdate
) eu on eu.controllerid = r.controllerid and eu.logDate = r.logdate
where sv.siteid in (8547, 8299, 8556, 8541, 8292, 8600, 8551, 5487, 8555, 8216, 8342, 8557, 8287, 8542, 8221, 5509, 8218, 8543, 8336, 8343) and r.logDate between '2016-01-01' and '2016-12-31'
group by sv.accountid, sv.companyName, sv.siteid, sv.siteName + '('+sv.siteCity + '-' + sv.siteState + ')',
datefromparts(datepart(yyyy,r.logdate),datepart(mm,r.logdate),1)
And I get the following error message from Microsoft Query:
No Column name was specified for column 3 of 'eu'.
Statement(s) could not be prepared.
I believe that "column 3" is referring to this specific part:
SUM((ISNULL(r.RuntimeSec, 0)/60.0) * ISNULL(s.EsrfGpm, 0)) as 'controllerEstKGal'
I've found cases where this question was asked before, but the replies say to put the name of column 3 in quotes. However (as you can see) I've done that and I'm still getting this error.
Help me StackExchange, you're my only hope! (and thank you!)
EDIT: To clarify, I get the error both with and without single/double quotes surrounding controllerEstKgal. Backticks result in the following error message:
Incorrect syntax near '`'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Statement(s) could not be prepared.

The conversion of the varchar value overflowed an int column in sql server 2012

I'm getting an error with a query to get data... The error message is: Msg 248, Level 16, State 1, Line 1: The conversion of the varchar value overflowed an int column...
I cant resolve this problem, so if any one can help me, thanks in advance, Here is My Sql:
This happens when I inserted 3 new joins in the query, I made them BOLD, otherwise it works perfectly...
SELECT DISTINCT
ACR_ART_ID, DES_TEXTS.TEX_TEXT AS CRITERIA_DES_TEXT,
COALESCE(DES_TEXTS2.TEX_TEXT, ACR_VALUE) AS CRITERIA_VALUE_TEXT,
(DES_TEXTS.TEX_TEXT + ': ' + COALESCE(DES_TEXTS2.TEX_TEXT, ACR_VALUE)) AS CEL_OPIS
FROM
Inventory.dbo.ARTICLE_CRITERIA
LEFT JOIN
Inventory.dbo.DESIGNATIONS AS DESIGNATIONS2 ON DESIGNATIONS2.DES_ID = ACR_KV_DES_ID
LEFT JOIN
Inventory.dbo.DES_TEXTS AS DES_TEXTS2 ON DES_TEXTS2.TEX_ID = DESIGNATIONS2.DES_TEX_ID
LEFT JOIN
Inventory.dbo.CRITERIA ON CRI_ID = ACR_CRI_ID
LEFT JOIN
Inventory.dbo.DESIGNATIONS ON DESIGNATIONS.DES_ID = CRI_DES_ID
LEFT JOIN
Inventory.dbo.DES_TEXTS ON DES_TEXTS.TEX_ID = DESIGNATIONS.DES_TEX_ID
INNER JOIN
Inventory.dbo.ART_LOOKUP al ON ARTICLE_CRITERIA.ACR_ART_ID = al.ARL_SEARCH_NUMBER
AND al.ARL_KIND in (1,3)
INNER JOIN
Inventory.dbo.ARTICLES a ON al.ARL_ART_ID = a.ART_ID and (a.ART_SUP_ID=21 or a.ART_SUP_ID=11091)
INNER JOIN
Inventory.dbo.SUPPLIERS ON SUPPLIERS.SUP_ID = ART_SUP_ID**
WHERE
(DESIGNATIONS.DES_LNG_ID IS NULL OR DESIGNATIONS.DES_LNG_ID = 25)
AND (DESIGNATIONS2.DES_LNG_ID IS NULL OR DESIGNATIONS2.DES_LNG_ID = 25);
As Commented by #T I
The varchar col is implicitly converted to an int and is overflowing (i.e. is >larger than 2,147,483,647). To resolve this try casting the columns to bigint
you can cast column value data type as
select cast( max(columnname) AS bigint)+1 from atable ;
But if you will get a varchar than it will fail,
with error
So it is better to change your DB schema.
Use this approach only if you are sure that only numeric string will be present.