SQL round function usage

Source: Internet
Author: User

-- Round
-- Returns a numeric expression rounded to the specified length or precision.
--
-- Syntax
-- Round (numeric_expression, length [, function])
--
-- Parameter
-- Numeric_expression
--
-- Expression of exact or approximate numeric data type (except BIT data type ).
--
-- Length
--
-- The precision of numeric_expression to be rounded. The length must be tinyint, smallint, or Int. When length is a positive number, numeric_expression is rounded to the specified decimal place. When length is a negative number, numeric_expression is rounded to the left of the decimal point specified by length.
--
-- Function
--
-- Is the type of operation to be executed. The function must be tinyint, smallint, or Int. If the value of function or function is omitted is 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.
--
-- Return type
-- Returns the same type as numeric_expression.
--
-- Comment
-- Round always returns a value. If length is a negative number and it is greater than the number before the decimal point, round returns 0.
--
-- Example result
-- Round (748.58,-4) 0
--
-- When length is a negative number, no matter what data type, round returns a rounding numeric_expression.
--
-- Example result
-- Round (748.58,-1) 750.00
-- Round (748.58,-2) 700.00
-- Round (748.58,-3) 1000.00
--
-- Example
-- A. Use round and estimated values
-- The following example shows two expressions, indicating that the round function is used and the last number is always an estimate.
--
Select round (123.9994, 3), round (123.9995, 3)
Go

-- The following is the result set:
--
----------
-- 123.9990 124.0000
--
-- B. Use the approximate values of round and rounding
-- The following example shows rounding and approximate values.
--
-- Statement result
Select round (123.4545, 2)
-- 123.4500

Select round (123.45,-2)
-- 100.00
--
-- C. Use round to intercept
-- The following example uses two select statements to explain the difference between rounding and truncation. The first statement rounds the result. The second statement truncates the result.
--
-- Statement result
Select round (150.75, 0)
-- 151.00

Select round (150.75, 0, 1)
-- 150.00

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.