0

I have two numbers stored in database as decimal with precision equals to 9. My objective is to add these two numbers and update the value in database.

DECLARE @v1 DECIMAL(9, 5), @v2 DECIMAL(9, 5)
SET @v1 = 9503.34000
SET @v2 = 1357.62000
SELECT CAST(@v1 + @v2 AS DECIMAL(9, 5))

When i add this in SQL it throws overflow error however on changing it to 10,5 it results in 10860.96000 .

How can i tell SQL to add and return result based on precision i want. I want this sum to return me 9,5 NOT 10,5.

2
  • 2
    Since the value of your sum is more than 10'000 - you cannot have it be DECIMAL(9,5) - this is a decimal number with a total of 9 digits, 5 of which are after the decimal point. That only leaves 4 digits before the decimal point, which is insufficient to hold your result. Commented May 5, 2012 at 14:36
  • Thanks marc, i realized that i need to change the design Commented May 5, 2012 at 15:19

1 Answer 1

4

It can't return DECIMAL(9,5). You have 5 digits in front of the decimal place as 9,503 + 1,357 is greater than 10,000.

If you only want 9 digits in total use CAST(@v1 +@v2 as DECIMAL(10,4)). You don't seem to use the 5th decimal place anyway.

0

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.