What is the usage of the SQL round function?
The SQL ROUND function is used to round a numeric field or expression to a specified precision. It takes two parameters: the value to round and the number of decimal places to retain.
Syntax:
ROUND(expression, number of decimal places)
Explanation of parameters:
- Values or expressions that need to be rounded to the nearest whole number.
- decimal_places: The number of decimal places to be retained.
Assume there is a table called “sales” with a numerical field named “amount”. We can use the ROUND function to round the values in the “amount” field to a specified number of decimal places.
Choose the sales amount rounded to two decimal places.
The statement will return the value of the amount field in the sales table, rounded to two decimal places.
It is important to note that the ROUND function has a special behavior: if the number of decimal places to keep is 0, the result returned will be an integer instead of a decimal number.