filmov
tv
Mathematical Functions in SQL SERVER Part II || ROUND, SQRT, SQUARE functions in SQL 2014
![preview_player](https://i.ytimg.com/vi/XnU87pxuI7s/maxresdefault.jpg)
Показать описание
This video will provide you the details of each Mathematical Function in SQL Server 2014.
***********************************************************************
Mathematical Functions Part I
*************************************************************************
ROUND (Transact-SQL)
Returns a numeric value, rounded to the specified length or precision.
Syntax
ROUND ( numeric_expression , length [ ,function ] )
Arguments
numeric_expression
Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.
length
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_expressionis rounded on the left side of the decimal point, as specified by length.
function
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_expressionis rounded. When a value other than 0 is specified, numeric_expression is truncated.
Return Types
Returns the following data types.
Expression result Return type
tinyint int
smallint int
int int
bigint bigint
decimal and numeric category (p, s) decimal(p, s)
money and smallmoney category money
float and real category float
Remarks
ROUND always returns a value. If length is negative and larger than the number of digits before the decimal point, ROUND returns 0.
Example Result
ROUND(748.58, -4) 0
ROUND returns a rounded numeric_expression, regardless of data type, when length is a negative number.
Examples Result
ROUND(748.58, -1) 750.00
ROUND(748.58, -2) 700.00
ROUND(748.58, -3) Results in an arithmetic overflow, because 748.58 defaults to decimal(5,2), which cannot return 1000.00.
To round up to 4 digits, change the data type of the input. For example:
SELECT ROUND(CAST (748.58 AS decimal (6,2)),-3); 1000.00
Examples
A. Using ROUND and rounding approximations
The following example shows rounding and approximations.
SELECT ROUND(123.4545, 2);
GO
SELECT ROUND(123.45, -2);
GO
*******************************************************************************
SQRT (Transact-SQL)
Returns the square root of the specified float value.
Syntax
SQRT ( float_expression )
Arguments
float_expression
Is an expression of type float or of a type that can be implicitly converted to float.
Return Types
float
*******************************************************************************
SQUARE (Transact-SQL)
Returns the square of the specified float value.
Syntax
SQUARE ( float_expression )
Arguments
float_expression
Is an expression of type float or of a type that can be implicitly converted to float.
Return Types
Float
***********************************************************************
Mathematical Functions Part I
*************************************************************************
ROUND (Transact-SQL)
Returns a numeric value, rounded to the specified length or precision.
Syntax
ROUND ( numeric_expression , length [ ,function ] )
Arguments
numeric_expression
Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.
length
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_expressionis rounded on the left side of the decimal point, as specified by length.
function
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_expressionis rounded. When a value other than 0 is specified, numeric_expression is truncated.
Return Types
Returns the following data types.
Expression result Return type
tinyint int
smallint int
int int
bigint bigint
decimal and numeric category (p, s) decimal(p, s)
money and smallmoney category money
float and real category float
Remarks
ROUND always returns a value. If length is negative and larger than the number of digits before the decimal point, ROUND returns 0.
Example Result
ROUND(748.58, -4) 0
ROUND returns a rounded numeric_expression, regardless of data type, when length is a negative number.
Examples Result
ROUND(748.58, -1) 750.00
ROUND(748.58, -2) 700.00
ROUND(748.58, -3) Results in an arithmetic overflow, because 748.58 defaults to decimal(5,2), which cannot return 1000.00.
To round up to 4 digits, change the data type of the input. For example:
SELECT ROUND(CAST (748.58 AS decimal (6,2)),-3); 1000.00
Examples
A. Using ROUND and rounding approximations
The following example shows rounding and approximations.
SELECT ROUND(123.4545, 2);
GO
SELECT ROUND(123.45, -2);
GO
*******************************************************************************
SQRT (Transact-SQL)
Returns the square root of the specified float value.
Syntax
SQRT ( float_expression )
Arguments
float_expression
Is an expression of type float or of a type that can be implicitly converted to float.
Return Types
float
*******************************************************************************
SQUARE (Transact-SQL)
Returns the square of the specified float value.
Syntax
SQUARE ( float_expression )
Arguments
float_expression
Is an expression of type float or of a type that can be implicitly converted to float.
Return Types
Float