Skip to content

Commit a1c4773

Browse files
authored
Merge pull request #33663 from rwestMSFT/rw-0331-fix-10049
Clarifying Transact-SQL ROUND behavior (PR 10049)
2 parents 5fcd06e + 8b9e454 commit a1c4773

File tree

1 file changed

+140
-107
lines changed

1 file changed

+140
-107
lines changed
Lines changed: 140 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: "ROUND (Transact-SQL)"
3-
description: "ROUND (Transact-SQL)"
3+
description: ROUND returns a numeric value, rounded to the specified length or precision.
44
author: MikeRayMSFT
55
ms.author: mikeray
6-
ms.date: "12/14/2017"
6+
ms.reviewer: randolphwest
7+
ms.date: 03/31/2025
78
ms.service: sql
89
ms.subservice: t-sql
910
ms.topic: reference
@@ -15,118 +16,150 @@ helpviewer_keywords:
1516
- "ROUND function [Transact-SQL]"
1617
dev_langs:
1718
- "TSQL"
18-
monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current||=fabric"
19+
monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric"
1920
---
2021
# ROUND (Transact-SQL)
22+
2123
[!INCLUDE [sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw.md)]
2224

23-
Returns a numeric value, rounded to the specified length or precision.
24-
25-
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
26-
27-
## Syntax
28-
25+
Returns a numeric value, rounded to the specified length or precision.
26+
27+
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
28+
29+
## Syntax
30+
2931
```syntaxsql
30-
ROUND ( numeric_expression , length [ ,function ] )
31-
```
32-
32+
ROUND ( numeric_expression , length [ , function ] )
33+
```
34+
3335
## Arguments
34-
*numeric_expression*
35-
Is an [expression](../../t-sql/language-elements/expressions-transact-sql.md) of the exact numeric or approximate numeric data type category.
36-
37-
*length*
38-
Is the precision to which *numeric_expression* is to be rounded. *length* must be an expression of type **tinyint**, **smallint**, or **int**. When *length* is a positive number, *numeric_expression* is rounded to the number of decimal positions specified by *length*. When *length* is a negative number, *numeric_expression* is rounded on the left side of the decimal point, as specified by *length*.
39-
40-
*function*
41-
Is the type of operation to perform. *function* must be **tinyint**, **smallint**, or **int**. When *function* is omitted or has a value of 0 (default), *numeric_expression* is rounded. When a value other than 0 is specified, *numeric_expression* is truncated.
42-
43-
## Return Types
44-
Returns the following data types.
45-
46-
|Expression result|Return type|
47-
|-----------------------|-----------------|
48-
|**tinyint**|**int**|
49-
|**smallint**|**int**|
50-
|**int**|**int**|
51-
|**bigint**|**bigint**|
52-
|**decimal** and **numeric** category (p, s)|**decimal(p, s)**|
53-
|**money** and **smallmoney** category|**money**|
54-
|**float** and **real** category|**float**|
55-
56-
## Remarks
57-
ROUND always returns a value. If *length* is negative and larger than the number of digits before the decimal point, ROUND returns 0.
58-
59-
|Example|Result|
60-
|-------------|------------|
61-
|ROUND(748.58, -4)|0|
62-
63-
ROUND returns a rounded *numeric_expression*, regardless of data type, when *length* is a negative number.
64-
65-
|Examples|Result|
66-
|--------------|------------|
67-
|ROUND(748.58, -1)|750.00|
68-
|ROUND(748.58, -2)|700.00|
69-
|ROUND(748.58, -3)|Results in an arithmetic overflow, because 748.58 defaults to decimal(5,2), which cannot return 1000.00.|
70-
|To round up to 4 digits, change the data type of the input. For example:<br /><br /> `SELECT ROUND(CAST (748.58 AS decimal (6,2)),-3);`|1000.00|
71-
72-
## Examples
73-
74-
### A. Using ROUND and estimates
75-
The following example shows two expressions that demonstrate by using `ROUND` the last digit is always an estimate.
76-
77-
```sql
78-
SELECT ROUND(123.9994, 3), ROUND(123.9995, 3);
79-
GO
80-
```
81-
82-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
83-
84-
```
85-
----------- -----------
86-
123.9990 124.0000
87-
```
88-
89-
### B. Using ROUND and rounding approximations
90-
The following example shows rounding and approximations.
91-
92-
```
93-
SELECT ROUND(123.4545, 2), ROUND(123.45, -2);
94-
```
95-
96-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
97-
98-
```
36+
37+
#### *numeric_expression*
38+
39+
An [expression](../../t-sql/language-elements/expressions-transact-sql.md) of the exact numeric or approximate numeric data type category.
40+
41+
#### *length*
42+
43+
The precision to which *numeric_expression* is to be rounded. *length* must be an expression of type **tinyint**, **smallint**, or **int**. When *length* is a positive number, *numeric_expression* is rounded to the number of decimal positions specified by *length*. When *length* is a negative number, *numeric_expression* is rounded on the left side of the decimal point, as specified by *length*.
44+
45+
#### *function*
46+
47+
The type of operation to perform. *function* must be **tinyint**, **smallint**, or **int**. When *function* is omitted or has a value of `0` (default), *numeric_expression* is rounded. When a value other than `0` is specified, *numeric_expression* is truncated.
48+
49+
## Return types
50+
51+
Returns the following data types.
52+
53+
| Expression result | Return type |
54+
| --- | --- |
55+
| **tinyint** | **int** |
56+
| **smallint** | **int** |
57+
| **int** | **int** |
58+
| **bigint** | **bigint** |
59+
| **decimal** and **numeric** category (p, s) | **decimal(p, s)** |
60+
| **money** and **smallmoney** category | **money** |
61+
| **float** and **real** category | **float** |
62+
63+
## Remarks
64+
65+
- `ROUND` always returns a value. If *length* is negative and larger than the number of digits before the decimal point, `ROUND` returns `0`.
66+
67+
| Example | Result |
68+
| --- | --- |
69+
| `ROUND(748.58, -4)` | 0 |
70+
71+
- `ROUND` returns a rounded *numeric_expression*, regardless of data type, when *length* is a negative number.
72+
73+
| Examples | Result |
74+
| --- | --- |
75+
| `ROUND(748.58, -1)` | 750.00 |
76+
| `ROUND(748.58, -2)` | 700.00 |
77+
| `ROUND(748.58, -3)` | Results in an arithmetic overflow, because 748.58 defaults to **decimal(5, 2)**, which can't return `1000.00`. |
78+
79+
- To round up to four digits, change the data type of the input. For example:
80+
81+
```sql
82+
SELECT ROUND(CAST (748.58 AS DECIMAL (6, 2)), -3);
83+
```
84+
85+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
86+
87+
```output
88+
1000.00
89+
```
90+
91+
- `ROUND` breaks ties by rounding half away from zero (also known as commercial rounding).
92+
93+
| Examples | Result |
94+
| --- | --- |
95+
| `ROUND(1.15, 1)` | 1.2 |
96+
| `ROUND(-1.15, 1)` | -1.2 |
97+
98+
## Examples
99+
100+
### A. Use ROUND and estimates
101+
102+
The following example shows two expressions that demonstrate by using `ROUND`, the last digit is always an estimate.
103+
104+
```sql
105+
SELECT ROUND(123.9994, 3),
106+
ROUND(123.9995, 3);
107+
GO
108+
```
109+
110+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
111+
112+
```output
113+
----------- -----------
114+
123.9990 124.0000
115+
```
116+
117+
### B. Use ROUND and rounding approximations
118+
119+
The following example shows rounding and approximations.
120+
121+
```sql
122+
SELECT ROUND(123.4545, 2),
123+
ROUND(123.45, -2);
124+
```
125+
126+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
127+
128+
```output
99129
---------- ----------
100130
123.4500 100.00
101131
```
102-
103-
### C. Using ROUND to truncate
104-
The following example uses two `SELECT` statements to demonstrate the difference between rounding and truncation. The first statement rounds the result. The second statement truncates the result.
105-
106-
```sql
107-
SELECT ROUND(150.75, 0);
108-
GO
109-
SELECT ROUND(150.75, 0, 1);
110-
GO
111-
```
112-
113-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
114-
115-
```
116-
--------
117-
151.00
118-
119-
(1 row(s) affected)
120-
121-
--------
122-
150.00
123-
124-
(1 row(s) affected)
132+
133+
### C. Use ROUND to truncate
134+
135+
The following example uses two `SELECT` statements to demonstrate the difference between rounding and truncation. The first statement rounds the result. The second statement truncates the result.
136+
137+
```sql
138+
SELECT ROUND(150.75, 0);
139+
GO
125140
```
126-
127-
## See Also
128-
[CEILING &#40;Transact-SQL&#41;](../../t-sql/functions/ceiling-transact-sql.md)
129-
[Data Types &#40;Transact-SQL&#41;](../../t-sql/data-types/data-types-transact-sql.md)
130-
[Expressions &#40;Transact-SQL&#41;](../../t-sql/language-elements/expressions-transact-sql.md)
131-
[FLOOR &#40;Transact-SQL&#41;](../../t-sql/functions/floor-transact-sql.md)
132-
[Mathematical Functions &#40;Transact-SQL&#41;](../../t-sql/functions/mathematical-functions-transact-sql.md)
141+
142+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
143+
144+
```output
145+
151.00
146+
```
147+
148+
```sql
149+
SELECT ROUND(150.75, 0, 1);
150+
GO
151+
```
152+
153+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
154+
155+
```output
156+
150.00
157+
```
158+
159+
## Related content
160+
161+
- [CEILING (Transact-SQL)](ceiling-transact-sql.md)
162+
- [Data types (Transact-SQL)](../data-types/data-types-transact-sql.md)
163+
- [Expressions (Transact-SQL)](../language-elements/expressions-transact-sql.md)
164+
- [FLOOR (Transact-SQL)](floor-transact-sql.md)
165+
- [Mathematical functions (Transact-SQL)](mathematical-functions-transact-sql.md)

0 commit comments

Comments
 (0)