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.
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;
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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.