数字操作函数和操作符

数字操作符

  • 描述:加

    示例:

    1. postgres=# SELECT 2+3 AS RESULT;
    2. result
    3. --------
    4. 5
    5. (1 row)
  • 描述:减

    示例:

    1. postgres=# SELECT 2-3 AS RESULT;
    2. result
    3. --------
    4. -1
    5. (1 row)
  • *

    描述:乘

    示例:

    1. postgres=# SELECT 2*3 AS RESULT;
    2. result
    3. --------
    4. 6
    5. (1 row)
  • /

    描述:除(除法操作符不会取整)

    示例:

    1. postgres=# SELECT 4/2 AS RESULT;
    2. result
    3. --------
    4. 2
    5. (1 row)
    1. postgres=# SELECT 4/3 AS RESULT;
    2. result
    3. ------------------
    4. 1.33333333333333
    5. (1 row)
  • +/-

    描述:正/负

    示例:

    1. postgres=# SELECT -2 AS RESULT;
    2. result
    3. --------
    4. -2
    5. (1 row)
  • %

    描述:模(求余)

    示例:

    1. postgres=# SELECT 5%4 AS RESULT;
    2. result
    3. --------
    4. 1
    5. (1 row)
  • @

    描述:绝对值

    示例:

    1. postgres=# SELECT @ -5.0 AS RESULT;
    2. result
    3. --------
    4. 5.0
    5. (1 row)
  • ^

    描述:幂(指数运算)

    示例:

    1. postgres=# SELECT 2.0^3.0 AS RESULT;
    2. result
    3. --------------------
    4. 8.0000000000000000
    5. (1 row)
  • |/

    描述:平方根

    示例:

    1. postgres=# SELECT |/ 25.0 AS RESULT;
    2. result
    3. --------
    4. 5
    5. (1 row)
  • ||/

    描述:立方根

    示例:

    1. postgres=# SELECT ||/ 27.0 AS RESULT;
    2. result
    3. --------
    4. 3
    5. (1 row)
  • !

    描述:阶乘

    示例:

    1. postgres=# SELECT 5! AS RESULT;
    2. result
    3. --------
    4. 120
    5. (1 row)
  • !!

    描述:阶乘(前缀操作符)

    示例:

    1. postgres=# SELECT !!5 AS RESULT;
    2. result
    3. --------
    4. 120
    5. (1 row)
  • &

    描述:二进制AND

    示例:

    1. postgres=# SELECT 91&15 AS RESULT;
    2. result
    3. --------
    4. 11
    5. (1 row)
  • |

    描述:二进制OR

    示例:

    1. postgres=# SELECT 32|3 AS RESULT;
    2. result
    3. --------
    4. 35
    5. (1 row)
  • #

    描述:二进制XOR

    示例:

    1. postgres=# SELECT 17#5 AS RESULT;
    2. result
    3. --------
    4. 20
    5. (1 row)
  • ~

    描述:二进制NOT

    示例:

    1. postgres=# SELECT ~1 AS RESULT;
    2. result
    3. --------
    4. -2
    5. (1 row)
  • <<

    描述:二进制左移

    示例:

    1. postgres=# SELECT 1<<4 AS RESULT;
    2. result
    3. --------
    4. 16
    5. (1 row)
  • >>

    描述:二进制右移

    示例:

    1. postgres=# SELECT 8>>2 AS RESULT;
    2. result
    3. --------
    4. 2
    5. (1 row)

数字操作函数

  • abs(x)

    描述:绝对值。

    返回值类型:和输入相同。

    示例:

    1. postgres=# SELECT abs(-17.4);
    2. abs
    3. ------
    4. 17.4
    5. (1 row)
  • acos(x)

    描述:反余弦。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT acos(-1);
    2. acos
    3. ------------------
    4. 3.14159265358979
    5. (1 row)
  • asin(x)

    描述:反正弦。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT asin(0.5);
    2. asin
    3. ------------------
    4. .523598775598299
    5. (1 row)
  • atan(x)

    描述:反正切。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT atan(1);
    2. atan
    3. ------------------
    4. .785398163397448
    5. (1 row)
  • atan2(y, x)

    描述:y/x的反正切。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT atan2(2, 1);
    2. atan2
    3. ------------------
    4. 1.10714871779409
    5. (1 row)
  • bitand(integer, integer)

    描述:计算两个数字与运算(&)的结果。

    返回值类型:bigint类型数字。

    示例:

    1. postgres=# SELECT bitand(127, 63);
    2. bitand
    3. --------
    4. 63
    5. (1 row)
  • cbrt(dp)

    描述:立方根。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT cbrt(27.0);
    2. cbrt
    3. ------
    4. 3
    5. (1 row)
  • ceil(x)

    描述:不小于参数的最小的整数。

    返回值类型:整数。

    示例:

    1. postgres=# SELECT ceil(-42.8);
    2. ceil
    3. ------
    4. -42
    5. (1 row)
  • ceiling(dp or numeric)

    描述:不小于参数的最小整数(ceil的别名)。

    返回值类型:与输入相同。

    示例:

    1. postgres=# SELECT ceiling(-95.3);
    2. ceiling
    3. ---------
    4. -95
    5. (1 row)
  • cos(x)

    描述:余弦。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT cos(-3.1415927);
    2. cos
    3. -------------------
    4. -.999999999999999
    5. (1 row)
  • cot(x)

    描述:余切。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT cot(1);
    2. cot
    3. ------------------
    4. .642092615934331
    5. (1 row)
  • degrees(dp)

    描述:把弧度转为角度。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT degrees(0.5);
    2. degrees
    3. ------------------
    4. 28.6478897565412
    5. (1 row)
  • div(y numeric, x numeric)

    描述:y除以x的商的整数部分。

    返回值类型:numeric

    示例:

    1. postgres=# SELECT div(9,4);
    2. div
    3. -----
    4. 2
    5. (1 row)
  • exp(x)

    描述:自然指数。

    返回值类型:与输入相同。

    示例:

    1. postgres=# SELECT exp(1.0);
    2. exp
    3. --------------------
    4. 2.7182818284590452
    5. (1 row)
  • floor(x)

    描述:不大于参数的最大整数。

    返回值类型:与输入相同。

    示例:

    1. postgres=# SELECT floor(-42.8);
    2. floor
    3. -------
    4. -43
    5. (1 row)
  • int1(in)

    描述:将传入的text参数转换为int1类型值并返回。

    返回值类型:int1

    示例:

    1. postgres=# select int1('123');
    2. int1
    3. ------
    4. 123
    5. (1 row)
    6. postgres=# select int1('a');
    7. int1
    8. ------
    9. 0
    10. (1 row)
  • int2(in)

    描述:将传入参数转换为int2类型值并返回。

    支持的入参类型包括float4,float8,numeric,text。

    返回值类型:int2

    示例:

    1. postgres=# select int2('1234');
    2. int2
    3. ------
    4. 1234
    5. (1 row)
    6. postgres=# select int2(25.3);
    7. int2
    8. ------
    9. 25
    10. (1 row)
  • int4(in)

    描述:将传入参数转换为int4类型值并返回。

    支持的入参类型包括bit,boolean,char,duoble precision,numeric,real,smallint,text。

    返回值类型:int4

    示例:

    1. postgres=# select int4('789');
    2. int4
    3. ------
    4. 789
    5. (1 row)
    6. postgres=# select int4(99.9);
    7. int4
    8. ------
    9. 99
    10. (1 row)
  • radians(dp)

    描述:把角度转为弧度。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT radians(45.0);
    2. radians
    3. ------------------
    4. .785398163397448
    5. (1 row)
  • random()

    描述:0.0到1.0之间的随机数。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT random();
    2. random
    3. ------------------
    4. .824823560658842
    5. (1 row)
  • multiply(x double precision or text, y double precision or text)

    描述:x和y的乘积。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT multiply(9.0, '3.0');
    2. multiply
    3. -------------------
    4. 27
    5. (1 row)
    6. postgres=# SELECT multiply('9.0', 3.0);
    7. multiply
    8. -------------------
    9. 27
    10. (1 row)
  • ln(x)

    描述:自然对数。

    返回值类型:与输入相同。

    示例:

    1. postgres=# SELECT ln(2.0);
    2. ln
    3. -------------------
    4. .6931471805599453
    5. (1 row)
  • log(x)

    描述:以10为底的对数。

    返回值类型:与输入相同。

    示例:

    1. postgres=# SELECT log(100.0);
    2. log
    3. --------------------
    4. 2.0000000000000000
    5. (1 row)
  • log(b numeric, x numeric)

    描述:以b为底的对数。

    返回值类型:numeric

    示例:

    1. postgres=# SELECT log(2.0, 64.0);
    2. log
    3. --------------------
    4. 6.0000000000000000
    5. (1 row)
  • mod(x,y)

    描述:x/y的余数(模)。如果x是0,则返回0。

    返回值类型:与参数类型相同。

    示例:

    1. postgres=# SELECT mod(9,4);
    2. mod
    3. -----
    4. 1
    5. (1 row)
    1. postgres=# SELECT mod(9,0);
    2. mod
    3. -----
    4. 9
    5. (1 row)
  • pi()

    描述:“π”常量。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT pi();
    2. pi
    3. ------------------
    4. 3.14159265358979
    5. (1 row)
  • power(a double precision, b double precision)

    描述:a的b次幂。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT power(9.0, 3.0);
    2. power
    3. ----------------------
    4. 729.0000000000000000
    5. (1 row)
  • round(x)

    描述:离输入参数最近的整数。

    返回值类型:与输入相同。

    示例:

    1. postgres=# SELECT round(42.4);
    2. round
    3. -------
    4. 42
    5. (1 row)
    6. postgres=# SELECT round(42.6);
    7. round
    8. -------
    9. 43
    10. (1 row)
  • round(v numeric, s int)

    描述:保留小数点后s位,s后一位进行四舍五入。

    返回值类型:numeric

    示例:

    1. postgres=# SELECT round(42.4382, 2);
    2. round
    3. -------
    4. 42.44
    5. (1 row)
  • setseed(dp)

    描述:为随后的random()调用设置种子(-1.0到1.0之间,包含)。

    返回值类型:void

    示例:

    1. postgres=# SELECT setseed(0.54823);
    2. setseed
    3. ---------
    4. (1 row)
  • sign(x)

    描述:输出此参数的符号。

    返回值类型:-1表示负数,0表示0,1表示正数。

    示例:

    1. postgres=# SELECT sign(-8.4);
    2. sign
    3. ------
    4. -1
    5. (1 row)
  • sin(x)

    描述:正弦。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT sin(1.57079);
    2. sin
    3. ------------------
    4. .999999999979986
    5. (1 row)
  • sqrt(x)

    描述:平方根。

    返回值类型:与输入相同。

    示例:

    1. postgres=# SELECT sqrt(2.0);
    2. sqrt
    3. -------------------
    4. 1.414213562373095
    5. (1 row)
  • tan(x)

    描述:正切。

    返回值类型:double precision

    示例:

    1. postgres=# SELECT tan(20);
    2. tan
    3. ------------------
    4. 2.23716094422474
    5. (1 row)
  • trunc(x)

    描述:截断(取整数部分)。

    返回值类型:与输入相同。

    示例:

    1. postgres=# SELECT trunc(42.8);
    2. trunc
    3. -------
    4. 42
    5. (1 row)
  • trunc(v numeric, s int)

    描述:截断为s位小数。

    返回值类型:numeric

    示例:

    1. postgres=# SELECT trunc(42.4382, 2);
    2. trunc
    3. -------
    4. 42.43
    5. (1 row)
  • smgrne(a smgr, b smgr)

    描述:比较两个smgr类型整数是否不相等。

    返回值类型:bool

  • smgreq(a smgr, b smgr)

    描述:比较两个smgr类型整数是否相等。

    返回值类型:bool

  • width_bucket(op numeric, b1 numeric, b2 numeric, count int)

    描述:返回一个桶,这个桶是在一个有count个桶,上界为b1下界为b2的等深柱图中operand将被赋予的那个桶。

    返回值类型:int

    示例:

    1. postgres=# SELECT width_bucket(5.35, 0.024, 10.06, 5);
    2. width_bucket
    3. --------------
    4. 3
    5. (1 row)
  • width_bucket(op dp, b1 dp, b2 dp, count int)

    描述:返回一个桶,这个桶是在一个有count个桶,上界为b1下界为b2的等深柱图中operand将被赋予的那个桶。

    返回值类型:int

    示例:

    1. postgres=# SELECT width_bucket(5.35, 0.024, 10.06, 5);
    2. width_bucket
    3. --------------
    4. 3
    5. (1 row)