Home  >  Article  >  Database  >  How to use round in sql

How to use round in sql

下次还敢
下次还敢Original
2024-05-02 02:24:171025browse

SQL ROUND function is used to round a number to the right of the decimal point for positive values ​​and to the left of the decimal point for negative values. When used, specify the number and digits to be rounded. For example, ROUND(123.456, 2) will round to two digits to the right of the decimal point, and the result is 123.46.

How to use round in sql

Usage of ROUND function in SQL

Introduction to ROUND function

## The #ROUND function rounds a number and rounds the result to a specified number of digits.

Syntax

<code>ROUND(number, decimals)</code>
Where:

  • number: The number to be rounded.
  • decimals: Number of digits to round. Positive values ​​represent the number of digits to the right of the decimal point, and negative values ​​represent the number of digits to the left of the decimal point.

How to use

To round a number, use the following steps:

    Determine the number of digits to round .
  1. Use the ROUND function in a SQL statement and specify the number and number of digits to round.
  2. Execute the SQL statement and store the results in variables or display them in the query results.

Example

Round to two digits to the right of the decimal point

<code>SELECT ROUND(123.456, 2);</code>
Result: 123.46

Round to the nearest whole number to the left of the decimal point

<code>SELECT ROUND(123456, -2);</code>
Result: 123000

Notes

    If
  • decimals is zero, the function rounds the number to the nearest integer.
  • If
  • decimals is positive, the function rounds the number to the specified number of digits to the right of the decimal point.
  • If
  • decimals is negative, the function rounds the number to the specified number of digits to the left of the decimal point.
  • If
  • number is NULL, the function returns NULL.

The above is the detailed content of How to use round in sql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to descend in sqlNext article:How to descend in sql