Here is my SQL statement
SELECT ROUND(AVG(column_name), 100) FROM [database].[dbo].[table]
The answer I retrieve is 3
with a possible range of numbers between 1 - 5
in 908 rows.
I need it to say 3.0
or 3.09
. Is this possible?
SQL CAST() inside AVG() for decimal valueThe SQL AVG() function returns the average value with default decimal places. The CAST() is used to increase or decrease the decimal places of a value. The CAST() function is much better at preserving the decimal places when converting decimal and numeric data types.
using this Custom format : #,0.
X - TRUNC(X), works for negatives too. It would give you the decimal part of the number, as a double, not an integer.
If you are in SQL Server, just use round(avg(column * 1.0), 0) .
The average will have the same data type as the values, so cast the values:
SELECT ROUND(AVG(CAST(column_name AS FLOAT)), 2) FROM [database].[dbo].[table]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With