This section describes the built-in mathematical scalar functions that can be used in SQL Server. These functions perform mathematical calculations on input values and return a single value.
Returns the absolute value of a numeric expression.
SELECT ABS(-10.5); -- Returns 10.5
Returns the smallest integer that is greater than or equal to a specified numeric expression.
SELECT CEILING(10.3); -- Returns 11
Converts an angle from radians to degrees.
SELECT DEGREES(PI()); -- Returns 180
Returns the exponentiation of the natural logarithm of the input value. That is, e raised to the power of the input value.
SELECT EXP(1); -- Returns 2.718281828459045... (e)
Returns the largest integer that is less than or equal to a specified numeric expression.
SELECT FLOOR(10.7); -- Returns 10
Returns the logarithm of a numeric expression. If base
is specified, it returns the logarithm of numeric_expression
to the specified base
. If base
is not specified, it returns the natural logarithm (base e).
SELECT LOG(100); -- Returns 4.605170185988092 (natural log)
SELECT LOG(10, 100); -- Returns 2 (log base 10)
Returns the base-10 logarithm of a numeric expression.
SELECT LOG10(1000); -- Returns 3
Returns the constant value of PI.
SELECT PI(); -- Returns 3.141592653589793
Returns the result of raising a numeric_expression
to the power of exponent
.
SELECT POWER(2, 3); -- Returns 8
Converts an angle from degrees to radians.
SELECT RADIANS(180); -- Returns 3.141592653589793 (PI)
Returns a pseudo-random floating-point value between 0 and 1. An optional seed
argument can be provided for reproducible results.
SELECT RAND(); -- Returns a random number between 0 and 1
SELECT RAND(123); -- Returns a repeatable random number
Rounds a numeric expression to a specified length or precision. The optional function
parameter determines rounding behavior (0 or omitted for rounding, non-zero for truncation).
SELECT ROUND(123.456, 2); -- Returns 123.46
SELECT ROUND(123.456, 0); -- Returns 123.00
SELECT ROUND(123.456, -2); -- Returns 100.00
SELECT ROUND(123.456, 2, 1); -- Returns 123.45 (truncation)
Returns the sign of a number: 1 if positive, -1 if negative, and 0 if zero.
SELECT SIGN(10); -- Returns 1
SELECT SIGN(-5); -- Returns -1
SELECT SIGN(0); -- Returns 0
Returns the sine of an angle (in radians).
SELECT SIN(PI()/2); -- Returns 1.0
Returns a four-character code that represents the sound of a name, based on the English pronunciation. Names that sound alike are encoded to the same code.
SELECT SOUNDEX('Smith'); -- Returns S530
SELECT SOUNDEX('Smyth'); -- Returns S530
Returns the square root of a numeric expression.
SELECT SQRT(16); -- Returns 4
Returns the tangent of an angle (in radians).
SELECT TAN(0); -- Returns 0.0
Returns a number truncated to an integer. It removes the fractional part of the number.
SELECT TRUNCATE(10.75); -- Returns 10