Mathematical Functions
Last updated
Last updated
Mathematical functions perform calculations based on input values provided as parameters to the functions, and return numeric values.
The mathematical functions covered in this section are:
A mathematical function that returns the absolute (positive) value of the specified numeric expression. (ABS
changes negative values to positive values. ABS
has no effect on zero or positive values.)
numeric_expression
An expression of the exact numeric or approximate numeric data type category.
Returns the same type as numeric_expression.
This example shows the results of using the ABS
function on three different numbers.
A function that returns the angle, in radians, whose cosine is the specified float expression. This is also called arccosine.
float_expression
An expression of either type float or of a type that can implicitly convert to float. Only a value ranging from -1.00 to 1.00 is valid. Values outside this range return NULL and ACOS will report a domain error.
float
This example returns the ACOS
value of the specified angle.
A function that returns the angle, in radians, whose sine is the specified float expression. This is also called arcsine.
float_expression
An expression of either type float or of a type that can implicitly convert to float. Only a value ranging from -1.00 to 1.00 is valid. Values outside this range return NULL and ASIN will report a domain error.
float
This example returns the ASIN
value of the specified angle.
A function that returns the angle, in radians, whose tangent is a specified float expression. This is also called arctangent.
float_expression
An expression of either type float or of a type that implicitly converts to float.
float
This example returns the ATAN
value of the specified angle.
Returns the angle, in radians, between the positive x-axis and the ray from the origin to the point (y, x), where x and y are the values of the two specified float expressions.
float_expression
An expression of data type float.
float
The following example calculates the ATN2
for the specified x
and y
components.
This function returns the smallest integer greater than, or equal to, the specified numeric expression.
numeric_expression
An expression of the exact numeric or approximate numeric data type category. For this function, the bit data type is invalid
Return values have the same type as numeric_expression.
This example shows positive numeric, negative numeric, and zero value inputs for the CEILING function.
A mathematical function that returns the trigonometric cosine of the specified angle - measured in radians - in the specified expression.
float_expression
An expression of type float.
float
This example returns the COS
value of the specified angle.
A mathematical function that returns the trigonometric cotangent of the specified angle - in radians - in the specified float expression.
float_expression
An expression of type float, or of a type that can implicitly convert to float.
float
This example returns the COT
value for the specific angle.
This function returns the corresponding angle, in degrees, for an angle specified in radians.
numeric_expression
An expression of the exact numeric or approximate numeric data type category, except for the bit data type.
Returns a value whose data type matches the data type of numeric_expression.
This example returns the number of degrees in a specified radian.
Returns the exponential value of the specified float expression.
float_expression
Is an expression of type float or of a type that can be implicitly converted to float.
float
The following example uses a compounding interest example to illustrate the use of EXP.
Returns the largest integer less than or equal to the specified numeric expression.
numeric_expression
Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.
Returns the same type as numeric_expression.
The following example shows positive numeric, negative numeric, and zero value inputs with the FLOOR
function.
Returns the natural logarithm of the specified float expression in SQL Server.
This function is not currently supported in PostgreSQL deployments of the Cinchy platform.
New function translations are actively being worked on by the development team; please check back at a later time.
You can review the full list of in-progress function translations here.
float_expression
Is an expression of type float or of a type that can be implicitly converted to float.
base
Optional integer argument that sets the base for the logarithm.
Applies to: SQL Server 2012 (11.x) and later
float
By default, LOG() returns the natural logarithm. Starting with SQL Server 2012 (11.x), you can change the base of the logarithm to another value by using the optional base parameter.
The natural logarithm is the logarithm to the base e, where e is an irrational constant approximately equal to 2.718281828.
The natural logarithm of the exponential of a number is the number itself: LOG( EXP( n ) ) = n. And the exponential of the natural logarithm of a number is the number itself: EXP( LOG( n ) ) = n.
The following example calculates the LOG
for a specified number.
Returns the base-10 logarithm of the specified float expression.
float_expression
Is an expression of type float or of a type that can be implicitly converted to float.
float
The LOG10 and POWER functions are inversely related to one another. For example, 10 ^ LOG10(n) = n.
Calculating the base 10 logarithm for a variable.
The following example calculates the LOG10
of the specified number.
Calculating the result of raising a base-10 logarithm to a specified power.
The following example returns the result of raising a base-10 logarithm to a specified power.
Returns the constant value of PI.
float
The following example returns the value of PI
.
Returns the value of the specified expression to the specified power.
float_expression
Is an expression of type float or of a type that can be implicitly converted to float.
y
Is the power to which to raise float_expression. y can be an expression of the exact numeric or approximate numeric data type category, except for the bit data type.
The return type depends on the input type of float_expression:
If the result does not fit in the return type, an arithmetic overflow error occurs.
The following example demonstrates raising a specified number to a specified power.
Returns radians when a numeric expression, in degrees, is entered.
This function is not currently supported in PostgreSQL deployments of the Cinchy platform.
New function translations are actively being worked on by the development team; please check back at a later time.
You can review the full list of in-progress function translations here.
numeric_expression
Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.
Returns the same type as numeric_expression.
The following example shows the number of radians based on a specified degree.
This function is not currently supported in PostgreSQL deployments of the Cinchy platform.
New function translations are actively being worked on by the development team; please check back at a later time.
You can review the full list of in-progress function translations here.
Returns a pseudo-random float value from 0 through 1, exclusive.
seed
Is an integer expression (tinyint, smallint, or int) that gives the seed value. If seed is not specified, the SQL Server Database Engine assigns a seed value at random. For a specified seed value, the result returned is always the same.
float
Repetitive calls of RAND() with the same seed value return the same results.
For one connection, if RAND() is called with a specified seed value, all subsequent calls of RAND() produce results based on the seeded RAND() call. For example, the following query will always return the same sequence of numbers.
Returns a numeric value, rounded to the specified length or precision.
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_expression is 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_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.
Returns the following data types.
ROUND always returns a value. If length is negative and larger than the number of digits before the decimal point, ROUND returns 0.
ROUND returns a rounded numeric_expression, regardless of data type, when length is a negative number.
Using ROUND and estimates
The following example shows two expressions that demonstrate by using ROUND
the last digit is always an estimate.
Using ROUND and rounding approximations
The following example shows rounding and approximations.
Using ROUND to truncate
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.
Returns the positive (+1), zero (0), or negative (-1) sign of the specified expression.
numeric_expression
Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.
The following example returns the SIGN values of a positive number, negative number, and zero.
Returns the trigonometric sine of the specified angle, in radians, and in an approximate numeric, float, expression.
float_expression
Is an expression of type float or of a type that can be implicitly converted to float, in radians.
float
The following example calculates the SIN for a specified angle.
Returns the square root of the specified float value.
float_expression
Is an expression of type float or of a type that can be implicitly converted to float.
float
The following example returns the square root of a number.
Returns the square of the specified float value.
float_expression
Is an expression of type float or of a type that can be implicitly converted to float.
float
The following example returns the square of a specified number.
Returns the tangent of the input expression.
float_expression
Is an expression of type float or of a type that can be implicitly converted to float, interpreted as a number of radians.
float
The following example returns the tangent of a specified angle.
ABS
COS
Input type
Return type
float, real
float
decimal(p, s)
decimal(38, s)
int, smallint, tinyint
int
bigint
bigint
money, smallmoney
money
bit, char, nchar, varchar, nvarchar
float
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
Examples
Results
ROUND(748.58, -4)
0
Examples
Restuls
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
Specified expression
Return type
bigint
bigint
int/smallint/tinyint
int
money/smallmoney
money
numeric/decimal
numeric/decimal
Other types
float