SQL AVG() to 2 decimals
Perhaps the ROUND()
function would work?
$sql3 = "SELECT ROUND(AVG(app_tests.test_resultPercent),2) FROM app_tests";
EDIT
Your PHP looks strange. You should either alias the average function as a column and access it by that name, or access it by index. Something like
echo "<h3>".$row3[0];
Set two decimal places after AVG function inside PIVOT
You want two decimal positions for all your numbers, even if these digits are not significant. This reads like a pure formatting issue.
I would recommend format()
:
SELECT Student,
format([English], '0.00') as [English],
format([Mathematics], '0.00') as [Mathematics],
format([Science], '0.00') as [Science],
format([Programming], '0.00') as [Programming],
format([History], '0.00') as [History]
FROM ...
Note that this turns the numbers to strings - which seems to be what you are actually asking here.
How to return a number with two decimal places in SQL Server without it automatically rounding
You can use floor()
and integer division:
select floor(8.23897666 * 100) / 100
Or better yet, use round()
with a non-0
third argument:
select round(8.23897666, 2, 1)
How do I retrieve decimals when rounding an average in SQL
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]
How to convert AVG() function to decimal in SQL SERVER
Here we take your INT value and add 0.0 which is a fast conversion to float. Then we apply the round() and convert()
Declare @YourTable table (Qtyshipped int)
Insert into @YourTable values
(2),
(3),
(15),
(3),
(10),
(9),
(10)
Select convert(decimal(8,6),round(Avg(Qtyshipped+0.0),2))
From @YourTable
Returns
7.430000
Related Topics
Sql Join: Selecting the Last Records in a One-To-Many Relationship
How to Get a List of Column Names on Sqlite3 Database
How to Create Column in SQL Query With Custom Text
How to Convert This SQL Select to Linq Query
Convert Utc Milliseconds to Datetime in SQL Server
Split and Get Second Row as Value
How to Get Textual Contents from Blob in Oracle SQL
Regex to Filter for Numers With and Without Dots
How to Sum Up Time Field in SQL Server
Sql 0 Results for 'Not In' and 'In' When Row Does Exist
Sql Query to Select Million Records Quickly
How to Select Three Table With Same Column Name But Different Values
How to Modify Query to Remove Double-Quotes from All Columns
Insert Multiple Rows Without Repeating the "Insert into ..." Part of the Statement
How to Subtract One Month from a Date Column
Left Join Without Duplicate Rows from Left Table
Sql Query to Return Only First Occurance of One Column Value
Sql Max Function Returns One Row When Multiple Rows Have the Same Value