SQL Round() Function: Explained With Examples

by | SQL

Databases and SQL frequently round numeric numbers. When working with huge data sets and calculations, numbers with high precision can make your data tougher to comprehend and interpret. SQL’s ROUND() function simplifies and presents data.

SQL’s built-in ROUND() function rounds numbers to decimal or integer precision. It lets you alter numerical data precision for presentation, analysis, and reporting. SQL queries with the ROUND() function can easily simplify complex numerical results. This makes data analysis more efficient and productive since you can focus on valuable insights rather than decimal points.

This article covers the SQL ROUND() function, its syntax, and its uses to alter numeric data. We will provide real examples to show how this adaptable function can help you get accurate results for your business needs.

Let’s dive in!

What is the SQL ROUND Function?

SQL ROUND function

This section will examine the SQL ROUND() function’s syntax, arguments, and application through examples.

Understanding ROUND() will improve your data manipulation skills and help you produce more accurate and understandable results, regardless of your SQL background.

Syntax for ROUND()

The SQL ROUND() rounds a numeric value to a specified number of decimal places or the nearest integer, depending on the inputs, making the data easier to understand.

ROUND(number, decimals, operation)
The syntax ROUND function

Arguments

  • number: The numeric value you want to round.
  • decimals: Number of decimal places to round to. It can be a positive, negative, or zero value.
  • operation (optional parameter): The type of rounding operation to perform. Tinyint, smallint, or int are required. Numbers are rounded or shortened, depending on the operation. The default value is 0

Examples

Here are some examples of how to use SQL ROUND():

SQL ROUND

Output:

SQL ROUND

Rounding Rules

SQL ROUND rounds as follows:

  • The function will round away from zero for positive numbers and down for negative numbers if the value to be rounded is exactly halfway between two possible values.
  • If no decimal places are supplied, the function rounds to the nearest integer.
  • The function rounds to the nearest multiple of 10, increasing to the absolute value of the specified decimal places if the integer is negative.

Rounding Techniques with the ROUND Function

Each SQL rounding option has its own functionality. These techniques include ROUND, TRUNCATE, CEILING, FLOOR, and the FLOOR function. Use the right rounding method for your use case.

1. ROUND

ROUND rounds numbers to the nearest integer or decimal place. The function’s decimals parameter controls decimal places.

SELECT ROUND(45.65, 1);
SQL ROUND()
SQL ROUND Output

2. TRUNCATE

Unlike ROUND, TRUNCATE rounds numbers. It removes your decimal places instead of rounding:

SELECT TRUNCATE(45.65, 1) -- Output: 45.6
SQL TRUNCATE
SQL TRUNCATE truncated ouput

3. CEILING

The CEILING function rounds up a number to its ceiling integer.

SELECT CEILING(45.65);
SQL CEILING
SQL CEILING()

4. FLOOR

FLOOR function returns and rounds down to the closest integer. It returns the greatest integer value smaller than the input value:

SELECT FLOOR(45.65);
SQL FLOOR
SQL FLOOR Output

With these methods and examples, you can reliably alter numeric input data in SQL to meet your needs while writing understandable code.

Working with Decimals and Precision

The ROUND() function accepts two arguments: the numeric data type to be rounded and the desired precision, which is the number of decimal digits in the rounded integer value.

This SQL query rounds the input value from an integer to two decimal places:

SELECT ROUND(your_column, 2) FROM your_table;

This query rounds your_column values to two decimals. Replace your_column and your_table with the database names.

The SQL server ROUND() function rounds values on the left side of the decimal point if precision is a negative integer. To illustrate:

SELECT ROUND(123.45, -1);
SQL ROUND()

Rounding to the closest tenth yields 120.

SQL ROUND()

If you wish to round 10.5 down to 10 without following the rounding rules, utilize the FLOOR() and CEILING() methods. These functions lack a precision argument like ROUND(); therefore, you must simulate them with multiplication and division.

Check out the following example:

Insert Data to Products
SELECT FLOOR(your_column * 100) / 100 AS rounded_down, CEILING(your_column * 100) / 100 AS rounded_up FROM your_table;
SQL FLOOR() and CEILING()

In the example table, the rounded_down and rounded_up columns will round your_column values to two decimal places. Again, use database names for your_column and your_table.

Output of SQL FLOOR() and CEILING() example

These methods and approaches help you manage decimal places and precision in SQL queries for clear and precise results.

Handling Numeric Expressions in SQL ROUND

The ROUND() function requires an input of a numeric_expression and a length or decimal place. It can determine the final price with a specified length and precision if you have a base price and a tax.

SELECT ROUND(price_net * (1 + tax/100), 2) AS price_gross FROM products;
SQL ROUND Function

The numeric_expression is the product of three parameters: the price_net, the tax %, and calculated by dividing the tax by 100. This expression calculates each item’s taxed gross price.

The ROUND() function’s second parameter, 2, rounds to two decimal places.

SQL Round Output

The ROUND() function rounds to the closest 10 if the precision parameter or length parameter is -1. ROUND(45.65, -1) yields 50.

For operations without decimal places, numeric_expression’s specified length argument will be rounded to the nearest integer if the precision parameter is omitted or set to 0.

The ROUND() function receives a base number, precision, and operation argument to control rounding. The operation argument defaults to 0, rounding to the nearest integer rather than truncating.

If the operation parameter is non-zero, the function truncates the numeric_expression to the left of the decimal point.

Round Function in SQL Queries

In a SELECT statement, the ROUND() function can round numbers to the nearest integer, decimal place, or tens or hundreds of digits.

The ROUND() method in a SELECT statement can round a number in a table column:

SELECT ROUND(column_name, decimal_places) as rounded_column FROM your_table;

Negative decimal_places values round numbers to the left of the decimal point. Use -1 as the length parameter to round a negative number to the nearest tens, like in the previous example.

SELECT ROUND(price, -1) as rounded_price FROM your_table;

The ROUND() function may truncate your findings. However, grouping or aggregating data can simplify analysis and presentation.

If you want to group your rounded values, you can use the ROUND() function along with the default GROUP BY clause. This following query will group and count rows with the same rounded values in the specified column.

SELECT ROUND(column_name, decimal_places) as rounded_column, COUNT(*) as count FROM your_table GROUP BY ROUND(column_name, decimal_places);

Error Handling and Overflow Using SQL ROUND

Arithmetic overflow issues might occur while using SQL ROUND. These errors occur when the function’s expected result doesn’t fit the data type.

If you round a DECIMAL(2,1) decimal number to one decimal point and get 10.0, you may get an arithmetic overflow error. The desired output is DECIMAL(3,1), but the specified data type doesn’t have enough capacity for it, causing an arithmetic overflow.

Preventing arithmetic overflow errors:

1. Correct data types: Provide the right data types for the anticipated result. Check the outcome range and choose a data type before rounding a number.

2. Casting: If ROUND overflows or errors, try casting. Cast your value to FLOAT or DECIMAL to meet the intended consequence. An example:

SELECT ROUND(CAST(748.58 AS FLOAT), -3);
SQL ROUND
SQL Round Output

3. Avoid rounding huge numbers: An arithmetic overflow may occur if the ROUND() parameter is excessively large. If feasible, lower the number size or avoid rounding.

These SQL ROUND() practices reduce arithmetic overflow issues and assure accurate and dependable query responses.

Final Thoughts

SQL ROUND

The SQL ROUND() function simplifies difficult numerical calculations and improves SQL database data display. This function effectively rounds numeric data values to the defined number of decimal places or the nearest integer, helping users focus on insights rather than precision.

This article addressed the syntax and usage of the SQL ROUND function and included real-world examples of its use. It also covered TRUNCATE, CEILING, and FLOOR rounding methods to help readers manage numeric data.

In conclusion, learning the SQL ROUND() function helps data professionals streamline data analysis, develop insights, and simplify data presentations. Users may unlock the full potential of their databases and confidently make data-driven decisions by using these powerful functions in SQL queries.

If you’d like to learn more about how to use SQL functions to streamline your workflow, check out the video below:

Frequently Asked Questions

How to round values to 2 decimal places in SQL?

SQL ROUND() rounds to 2 decimal places. Rounding and decimal places are the function’s arguments. The following example is, to round a value to 2 decimal places:

SELECT ROUND(123.456, 2);
SQL ROUND() syntax

This query yields 123.46.

SQL ROUND() Output

What is the difference between the ROUND, CEILING, and FLOOR functions in SQL?

ROUND() rounds a value to a specified number of decimal places using normal rounding techniques. CEILING() rounds up, FLOOR() rounds down. Here’s an example for each function:

SELECT ROUND(45.65, 1); 
Result: 45.7.
SQL ROUND() on different values
SQL ROUND Output
SELECT CEILING(45.65); 
Result: 46.
SQL CEILING()
SQL CEILING()
 SELECT FLOOR(45.65);  
Result: 45.
SQL FLOOR()
SQL FLOOR Output

How to round up to the nearest whole number in SQL?

To round up to the nearest whole number in SQL, use the CEILING() function. For the following example:

SELECT CEILING(45.65);
SQL CEILING()

This query would return 46.

Rounded number output of SQL CEILING()

How to display two decimal places without rounding in SQL?

To Round the number to 2 decimal places of a given number without rounding in SQL, you can use the FORMAT() function (available in some SQL platforms like Microsoft SQL Server). For example:

SELECT FORMAT(3.14159, '2');
SQL FORMAT()

This query would return 3.14 without rounding up to 3.15.

SQL FORMAT() output

How does the ROUND function work on different SQL platforms?

Most SQL platforms have the ROUND() function; however, some have syntax variations or additional parameters. SQL Server’s ROUND() function allows a third parameter to adjust rounding. Use the SQL platform’s documentation to verify accuracy.

How to round values to a specific number in SQL?

Use the ROUND() function and a negative or positive integer to round data in SQL. Rounding to the closest 10:

SELECT ROUND(25, -1);
SQL ROUND()

This query would return 30.

SQL ROUND()
author avatar
Sam McKay, CFA
Sam is Enterprise DNA's CEO & Founder. He helps individuals and organizations develop data driven cultures and create enterprise value by delivering business intelligence training and education.

Related Posts