3

Error: Arithmetic overflow error converting expression to data type nvarchar.

UPDATE [dbo].ForecastAccuracyKeyAccounts
SET ThreeMonthPercent = ((Actual - ThreeMonthForecast) / Actual) * 100, SixMonthPercent = ((Actual - SixMonthForecast) / Actual) * 100, 
NineMonthPercent = ((Actual - NineMonthForecast) / Actual) * 100
WHERE Actual != 0

Since, I am dividing by Actual, I want to ensure that Actual is not equal to zero. But when I add the statement in the WHERE CLAUSE I cannot get rid of the error.

7
  • what data types are your columns? It looks like there is something funky going on with type conversions Commented Feb 10, 2014 at 20:00
  • What database are you using? Commented Feb 10, 2014 at 20:04
  • Actual float; ThreeMonthPercent nvarchar(7); ThreeMonthForecast float; This worked fine before I added the Where Clause. The only error I would get is when the Actual column was 0. Commented Feb 10, 2014 at 20:04
  • Can you show the values that are causing the expression to fail? Commented Feb 10, 2014 at 20:17
  • I'm not entirely sure ... but does SQL Server guarantee the order of processing (WHERE before SET)? If not, it could evaluate the SET statements first before applying the WHERE clause. [In which case you'll need to test for Actual=0 somehow in your SET statement, e.g. ... /NULLIF(Actual,0)] Commented Feb 10, 2014 at 20:38

1 Answer 1

1

Maybe try ISNULL

   

UPDATE [dbo].ForecastAccuracyKeyAccounts

    SET ThreeMonthPercent = ((Actual - ISNULL(ThreeMonthForecast,0)) / Actual) * 100,

    SixMonthPercent = ((Actual - ISNULL(SixMonthForecast,0)) / Actual) * 100, 

    NineMonthPercent = ((Actual - ISNULL(NineMonthForecast,0)) / Actual) * 100

    WHERE Actual != 0
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry about the formatting, i'm posting from my mobile phone. You have a NULL value for ThreeMonthForecast. That is the problem. Using IsNull should fix it.
This does not work, I still get the overflow error. Thank you for your help though! It's tough typing from a cellphone!
Can you increase the size of your nvarchar(7) and see if that helps.
I support change the data type as a whole? This is a percentage, so maybe change it to decimal?
Change your ThreeMonthPercent, SixmonthPercent and NineMonthPercent to float and see if that helps.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.