现在位置:首页 > 数据库 > PostgreSQL > PostgreSQL数字函数

PostgreSQL数字函数

来源:原创文章    由 易百 更新版本    浏览:人次

PostgreSQL的数字函数主要用于数字操纵和/或数学计算。下表详列的数字函数:

yiibai.com

名称 描述
ABS() Returns the absolute value of numeric expression.
ACOS() Returns the arccosine of numeric expression. Returns NULL if the value is not in the range -1 to 1.
ASIN() Returns the arcsine of numeric expression. Returns NULL if value is not in the range -1 to 1
ATAN() Returns the arctangent of numeric expression.
ATAN2() Returns the arctangent of the two variables passed to it.
CEIL() Returns the smallest integer value that is not less than passed numeric expression
CEILING() Returns the smallest integer value that is not less than passed numeric expression
COS() Returns the cosine of passed numeric expression. The numeric expression should be expressed in radians.
COT() Returns the cotangent of passed numeric expression.
DEGREES() Returns numeric expression converted from radians to degrees.
EXP() Returns the base of the natural logarithm (e) raised to the power of passed numeric expression.
FLOOR() Returns the largest integer value that is not greater than passed numeric expression.
GREATEST() Returns the largest value of the input expressions.
LEAST() Returns the minimum-valued input when given two or more.
LOG() Returns the natural logarithm of the passed numeric expression.
MOD() Returns the remainder of one expression by diving by another expression.
PI() Returns the value of pi
POW() Returns the value of one expression raised to the power of another expression
POWER() Returns the value of one expression raised to the power of another expression
RADIANS() Returns the value of passed expression converted from degrees to radians.
ROUND() Returns numeric expression rounded to an integer. Can be used to round an expression to a number of decimal points
SIN() Returns the sine of numeric expression given in radians.
SQRT() Returns the non-negative square root of numeric expression.
TAN() Returns the tangent of numeric expression expressed in radians.

ABS(X)

The ABS() function returns the absolute value of X. Consider the following example:

yiibai.com

testdb=# SELECT ABS(2);
+---------------------------------------------------------+
| ABS(2)                                                  |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=# SELECT ABS(-2);
+---------------------------------------------------------+
| ABS(2)                                                  |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec) yiibai.com 

ACOS(X)

This function returns the arccosine of X. The value of X must range between .1 and 1 or NULL will be returned. Consider the following example:

yiibai.com

testdb=# SELECT ACOS(1);
+---------------------------------------------------------+
| ACOS(1)                                                 |
+---------------------------------------------------------+
| 0.000000                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec) 
yiibai.com

ASIN(X)

The ASIN() function returns the arcsine of X. The value of X must be in the range of .1 to 1 or NULL is returned.

www.yiibai.com

testdb=# SELECT ASIN(1);
+---------------------------------------------------------+
| ASIN(1)                                                 |
+---------------------------------------------------------+
| 1.5707963267949                                         |
+---------------------------------------------------------+
1 row in set (0.00 sec) yiibai.com 

ATAN(X)

This function returns the arctangent of X. yiibai.com

testdb=# SELECT ATAN(1);
+---------------------------------------------------------+
| ATAN(1)                                                 |
+---------------------------------------------------------+
| 0.78539816339745                                        |
+---------------------------------------------------------+
1 row in set (0.00 sec) 

yiibai.com

ATAN2(Y,X)

This function returns the arctangent of the two arguments: X and Y. It is similar to the arctangent of Y/X, except that the signs of both are used to find the quadrant of the result. www.yiibai.com

testdb=# SELECT ATAN2(3,6);
+---------------------------------------------------------+
| ATAN2(3,6)                                              |
+---------------------------------------------------------+
| 0.46364760900081                                        |
+---------------------------------------------------------+
1 row in set (0.00 sec) www.yiibai.com 

CEIL(X)

CEILING(X)

These function return the smallest integer value that is not smaller than X. Consider the following example:

yiibai.com

testdb=# SELECT CEILING(3.46);
+---------------------------------------------------------+
| CEILING(3.46)                                           |
+---------------------------------------------------------+
| 4                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=# SELECT CEIL(-6.43);
+---------------------------------------------------------+
| CEIL(-6.43)                                             |
+---------------------------------------------------------+
| -6                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec) yiibai.com 

COS(X)

This function returns the cosine of X. The value of X is given in radians. www.yiibai.com

testdb=#SELECT COS(90);
+---------------------------------------------------------+
| COS(90)                                                 |
+---------------------------------------------------------+
| -0.44807361612917                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec) yiibai.com 

COT(X)

This function returns the cotangent of X. Consider the following example: yiibai.com

testdb=#SELECT COT(1);
+---------------------------------------------------------+
| COT(1)                                                  |
+---------------------------------------------------------+
| 0.64209261593433                                        |
+---------------------------------------------------------+
1 row in set (0.00 sec) www.yiibai.com 

DEGREES(X)

This function returns the value of X converted from radians to degrees.

yiibai.com

testdb=#SELECT DEGREES(PI());
+---------------------------------------------------------+
| DEGREES(PI())                                           |
+---------------------------------------------------------+
| 180.000000                                              |
+---------------------------------------------------------+
1 row in set (0.00 sec) 
yiibai.com

EXP(X)

This function returns the value of e (the base of the natural logarithm) raised to the power of X.

www.yiibai.com

testdb=#SELECT EXP(3);
+---------------------------------------------------------+
| EXP(3)                                                  |
+---------------------------------------------------------+
| 20.085537                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec) 

yiibai.com

FLOOR(X)

This function returns the largest integer value that is not greater than X.

yiibai.com

testdb=#SELECT FLOOR(7.55);
+---------------------------------------------------------+
| FLOOR(7.55)                                             |
+---------------------------------------------------------+
| 7                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec) yiibai.com 

GREATEST(n1,n2,n3,..........)

The GREATEST() function returns the greatest value in the set of input parameters (n1, n2, n3, and so on). The following example uses the GREATEST() function to return the largest number from a set of numeric values:

yiibai.com

testdb=#SELECT GREATEST(3,5,1,8,33,99,34,55,67,43);
+---------------------------------------------------------+
| GREATEST(3,5,1,8,33,99,34,55,67,43)                     |
+---------------------------------------------------------+
| 99                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec) 
yiibai.com

LEAST(N1,N2,N3,N4,......)

The LEAST() function is the opposite of the GREATEST() function. Its purpose is to return the least-valued item from the value list (N1, N2, N3, and so on). The following example shows the proper usage and output for the LEAST() function:

yiibai.com

testdb=#SELECT LEAST(3,5,1,8,33,99,34,55,67,43);
+---------------------------------------------------------+
| LEAST(3,5,1,8,33,99,34,55,67,43)                        |
+---------------------------------------------------------+
| 1                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec) www.yiibai.com 

LOG(X)

LOG(B,X)

The single argument version of the function will return the natural logarithm of X. If it is called with two arguments, it returns the logarithm of X for an arbitrary base B. Consider the following example:

www.yiibai.com

testdb=#SELECT LOG(45);
+---------------------------------------------------------+
| LOG(45)                                                 |
+---------------------------------------------------------+
| 1.65321251377534                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=#SELECT LOG(2,65536);
+---------------------------------------------------------+
| LOG(2,65536)                                            |
+---------------------------------------------------------+
| 16.000000                                               |
+---------------------------------------------------------+
1 row in set (0.00 sec) 
yiibai.com

MOD(N,M)

This function returns the remainder of N divided by M. Consider the following example: www.yiibai.com

testdb=#SELECT MOD(29,3);
+---------------------------------------------------------+
| MOD(29,3)                                               |
+---------------------------------------------------------+
| 2                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec) 

yiibai.com

PI()

This function simply returns the value of pi. SQL internally stores the full double-precision value of pi.

www.yiibai.com

testdb=#SELECT PI();
+---------------------------------------------------------+
| PI()                                                    |
+---------------------------------------------------------+
| 3.141593                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec) 
www.yiibai.com

POW(X,Y)

POWER(X,Y)

These two functions return the value of X raised to the power of Y. www.yiibai.com

testdb=# SELECT POWER(3,3);
+---------------------------------------------------------+
| POWER(3,3)                                              |
+---------------------------------------------------------+
| 27                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec) 
yiibai.com

RADIANS(X)

This function returns the value of X, converted from degrees to radians. www.yiibai.com

testdb=#SELECT RADIANS(90);
+---------------------------------------------------------+
| RADIANS(90)                                             |
+---------------------------------------------------------+
|1.570796                                                 |
+---------------------------------------------------------+
1 row in set (0.00 sec) www.yiibai.com 

ROUND(X)

ROUND(X,D)

This function returns X rounded to the nearest integer. If a second argument, D, is supplied, then the function returns X rounded to D decimal places. D must be positive or all digits to the right of the decimal point will be removed. Consider the following example:

yiibai.com

testdb=#SELECT ROUND(5.693893);
+---------------------------------------------------------+
| ROUND(5.693893)                                         |
+---------------------------------------------------------+
| 6                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=#SELECT ROUND(5.693893,2);
+---------------------------------------------------------+
| ROUND(5.693893,2)                                       |
+---------------------------------------------------------+
| 5.69                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec) www.yiibai.com 

SIGN(X)

This function returns the sign of X (negative, zero, or positive) as .1, 0, or 1. yiibai.com

testdb=#SELECT SIGN(-4.65);
+---------------------------------------------------------+
| SIGN(-4.65)                                             |
+---------------------------------------------------------+
| -1                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=#SELECT SIGN(0);
+---------------------------------------------------------+
| SIGN(0)                                                 |
+---------------------------------------------------------+
| 0                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=#SELECT SIGN(4.65);
+---------------------------------------------------------+
| SIGN(4.65)                                              |
+---------------------------------------------------------+
| 1                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec) 

yiibai.com

SIN(X)

This function returns the sine of X. Consider the following example:

yiibai.com

testdb=#SELECT SIN(90);
+---------------------------------------------------------+
| SIN(90)                                                 |
+---------------------------------------------------------+
| 0.893997                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec) 
yiibai.com

SQRT(X)

This function returns the non-negative square root of X. Consider the following example:

www.yiibai.com

testdb=#SELECT SQRT(49);
+---------------------------------------------------------+
| SQRT(49)                                                |
+---------------------------------------------------------+
| 7                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec) www.yiibai.com 

TAN(X)

This function returns the tangent of the argument X, which is expressed in radians.

yiibai.com

testdb=#SELECT TAN(45);
+---------------------------------------------------------+
| TAN(45)                                                 |
+---------------------------------------------------------+
| 1.619775                                                |
+---------------------------------------------------------+
1 row in set (0.00 sec) www.yiibai.com 
本站文章除注明转载外,均为本站原创或编译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,传播学习教程;
转载请注明:文章转载自:易百教程 [http://www.yiibai.com]
本文标题:PostgreSQL数字函数
转载请保留原文链接:http://www.yiibai.com/html/postgresql/2013/080892.html
上一篇:PostgreSQL Array函数      下一篇:PostgreSQL字符串函数