0

So, here's my query:

SELECT '$'
       + CONVERT(VARCHAR (6), Cast(Avg(TotalPrice) AS NUMERIC (6, 2))) AS 'Average Price',
       '$'
       + CONVERT(VARCHAR (6), Cast(Min(TotalPrice) AS NUMERIC (6, 2))) AS 'Minimum Price',
       '$'
       + CONVERT(VARCHAR (6), Cast(Max(TotalPrice) AS NUMERIC (6, 2))) AS 'Maximum Price'
FROM   Invoice; 

The AVG column and the MIN column work fine but the MAX column returns:

"Arithmetic overflow error converting numeric to data type varchar"

And I'm not sure why I get the error.

3 Answers 3

2

The NUMERIC(6,2) indicates total 6 digits and out of which 2 are decimal places.

you have a value like 1234.66 then total characters needed is 7

Get the maximum value and use appropriate varchar size, here you need atleast varchar(7)

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

SELECT '$'
       + CONVERT(VARCHAR (10), Cast(Avg(TotalPrice) AS NUMERIC (8, 2))) AS 'Average Price',
       '$'
       + CONVERT(VARCHAR (10), Cast(Min(TotalPrice) AS NUMERIC (8, 2))) AS 'Minimum Price',
       '$'
       + CONVERT(VARCHAR (10), Cast(Max(TotalPrice) AS NUMERIC (8, 2))) AS 'Maximum Price'
FROM   Invoice; 

3 Comments

Why does the OP need to change the NUMERIC declaration from (6,2) to (8,2)?
Beacuse in NUMERIC (6, 2) (precision = 6) The maximum total number of decimal digits that will be stored, both to the left and to the right of the decimal point. so precision has to increased.
There isn't any indication that MAX(TotalPrice) is exceeding NUMERIC(6,2).
1

Your problem is that a 'Numeric(6,2)' has up to 6 digits, plus a decimal point (or comma depending on where you are). So you will need to have 'VARCHAR(7)' instead of 6 to cater for this.

Comments

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.