How can I know which value raise the error `Arithmetic overflow error converting numeric to data type numeric.` - sql-server

I'm doing an ETL processes with some SQL Server procedures.
WITH CTE (qty_top, qty_lvl) AS
(
SELECT
CONVERT(decimal(20,6), t.unity),
CONVERT(decimal(20,6), t.unity)
FROM
top_tab
UNION ALL
SELECT
CONVERT(decimal(20,6), cte.qty * t.unity),
CONVERT(decimal(20,6), nom.Nomenclature_unite_production)
FROM
CTE_NOM cte
JOIN
tab t
)
This is my SQL Request (very simplified). When I do this, this raise me an Arithmetic overflow error converting numeric to data type numeric.
I tried to change my decimal(20,6) into a bigger number (30,6) but it still doesn't work.
How can I know which values raise me this error ? So It can help me to resolve this error.

Related

Error converting data type when using multiple WHERE statements

I have a SQL query that works fine when the WHERE statement contains only one statement, but not when it contains both statements at the same time. When I use both WHERE statements, I get "Error converting data type to varchar to numeric." (The error is occurring in the second SELECT statement, not in the CTE).
This code is what I want:
;WITH cte_1 as(
SELECT EVENT_ID
,CLIENT_ID
,convert(varchar(10), E.STARTDATE, 101) as R106_DATE
FROM SCEVENT
WHERE STARTDATE>='06/01/2016' AND STARTDATE<='06/30/2016'
and SVC_ID=106
)
SELECT R.*
,convert(varchar(10), S.BEG_DATE, 101) as SVC_DATE
,S.UNIT_ID
FROM cte_1 R
JOIN CDCLSVC S
on R.CLIENT_ID=S.CLIENT_ID
WHERE R106_DATE<=BEG_DATE
and UNIT_ID=251
If I include one or the other WHERE statements, it works fine. But if I have both of those statements, I get an error.
(The desired result is a list of people who had a service at unit 251 after they had a service code (SVC_ID) 106.)
Thoughts?
I changed the code so that it's not converting the date until after the match (excellent point, #JonathanShields) but I kept getting the error message.
I ended up changing my date range to a longer period of time, and suddenly it worked. I guess there were no records in the given time frame that met both criteria, but instead of returning a result of 0 records it gave me the error message.
Thanks for everyone's help.

Error in stored procedure but no error when running it manually

I have a stored procedure that creates a xml file. When I run it, I get an error
Conversion failed when converting the varchar value 'some text here' to int
If I put the same code in the query window in SSMS and execute it, it works fine - no error.
Can someone tell me why could that happen? The procedure is long enough to post it here, it's mainly a huge select with lots of unions and joins.
Thank you!
Somewhere in one of the columns in your join is comparing a varchar to an integer, and at least one of the varchar values is not an integer.
So try the following test:
select *
from table
where myCol NOT LIKE '%[^0-9]%'
and perhaps
select *
from table
where CONVERT (bigint, myCol) NOT BETWEEN -2147483648 AND 2147483647
Shake the tree and see what falls off.

Converting varchar to number

I recently did a TO_NUMBER(column_name) and it converted it to a number successfully. My question is in regards to the data type, it is data type NUMBER in Oracle, originally the column is a varchar. Is it possible to do a TO_NUMBER conversion that would result in a data type of NUMBER(19,0) rather than just a NUMBER data type?
Here is my statement:
select column1, cast(regexp_replace(column1, 'hi') as NUMBER(19,0)) TEST from table1
However when I try to create a view with this I get:
Error(s) parsing SQL: Unexpected token at 63 near ,0)) TEST from table1.
Unexpected token at 65 near )) TEST from table1.
Is cast not possible to create a view?.
Thanks,
Use CAST function: CAST(TO_NUMBER(column_name) AS NUMBER(19, 0))

ms sql server executes 'then' before 'when' in case

when i try to select
select
case when (isnumeric(SUBSTRING([VZWECK2],1,9)) = 1)
then CONVERT(decimal,SUBSTRING([VZWECK2],1,9))
else null
end as [NUM]
from table
sql-server gives me:
Msg 8114, Level 16, State 5, Line 2
Error converting data type varchar to numeric.
[VZWECK2] is a char(27). is this a known bug? because it seems to me it executes the convert before it does the case, which defies the purpose of my select. i know that there are values that are not numeric obviously, which is why i need the case statement to weed them out.
for some reason selecting
select
case when (isnumeric(SUBSTRING([VZWECK2],1,9)) = 1)
then 99
else null
end as [NUM]
from table
yields no errors and behaves as expected
The problem is that ISNUMERIC is very forgiving, and that ISNUMERIC returns 1 is unfortunately no guarantee that CONVERT will work. This is why SQL Server 2012 and later introduced TRY_CAST and TRY_CONVERT.
If you are converting whole numbers, a more reliable check is to make sure the string consists of only digits with NOT LIKE '%[^0-9]%' (that is, it must not contain a non-digit anywhere). This is too restrictive for some formats (like floating point) but for integers it works nicely.
Do you know the value which throws the error? IsNumeric is not exactly fool-proof, for example:
select ISNUMERIC('$')
select ISNUMERIC('+')
select ISNUMERIC('-')
all yield 1
Alternatively, you could go with TRY_PARSE instead.
Edit: TRY_PARSE is introduced in sql server 2012, so may not be available to you.

Encountering SQL Error not having to do with my query... what can I do to get to my data?

I'm trying to run this query:
select * from activity where id = 9348927
And I receive this error:
Conversion failed when converting the varchar value '04 30' to data type int.
Note that I still receive this error when I change * to id, which doesn't make any sense to me. I don't receive the error when I run this:
select top 32 * from activity where id = 9348927
I'm pretty sure this means that some of the data on the 33rd row is in a format that SQL Server doesn't like. Is there any way I can get it to ignore the error and display my data anyway?
EDIT: id is varchar(10)
This usually means that the id column is not a number datatype, but varchar.
9348927 is a number (int) and datatype precedence means the the string value '04 30' is converted to int. Standard SQL behaviour.
Try this:
select * from activity where id = '9348927'
Incidently, the implicit conversion means any index on id will not be used. Compare query plans.
The best way to "work around" this is to fix your data and column type. Otherwise, do the comparison as string which is what id most likely is.

Resources