数字操作函数和操作符

数字操作符

  • +

描述:加

示例:

1. openGauss=# SELECT 2+3 AS RESULT; 2. result 3. -------- 4. 5 5. (1 row) - -

描述:减

示例:

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

描述:乘

示例:

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

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

示例:

1. openGauss=# SELECT 4/2 AS RESULT; 2. result 3. -------- 4. 2 5. (1 row)

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

描述:正/负

示例:

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

描述:模(求余)

示例:

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

描述:绝对值

示例:

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

描述:幂(指数运算)

示例:

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

描述:平方根

示例:

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

描述:立方根

示例:

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

描述:阶乘

示例:

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

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

示例:

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

描述:二进制AND

示例:

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

描述:二进制OR

示例:

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

描述:二进制XOR

示例:

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

描述:二进制NOT

示例:

1. openGauss=# SELECT ~1 AS RESULT; 2. result 3. -------- 4. -2 5. (1 row) - «

描述:二进制左移

示例:

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

描述:二进制右移

示例:

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

数字操作函数

  • abs(x)

描述:绝对值。

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

示例:

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

描述:反余弦。

返回值类型:double precision

示例:

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

描述:反正弦。

返回值类型:double precision

示例:

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

描述:反正切。

返回值类型:double precision

示例:

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

描述:y/x的反正切。

返回值类型:double precision

示例:

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

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

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

示例:

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

描述:立方根。

返回值类型:double precision

示例:

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

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

返回值类型:整数。

示例:

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

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

返回值类型:dp or numeric,不考虑隐式类型转换的情况下与输入相同。

示例:

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

描述:余弦。

返回值类型:double precision

示例:

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

描述:余切。

返回值类型:double precision

示例:

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

描述:把弧度转为角度。

返回值类型:double precision

示例:

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

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

返回值类型:numeric

示例:

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

描述:自然指数。

返回值类型:dp or numeric,不考虑隐式类型转换的情况下与输入相同。

示例:

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

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

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

示例:

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

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

返回值类型:int1

示例:

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

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

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

返回值类型:int2

示例:

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

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

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

返回值类型:int4

示例:

1. openGauss=# select int4('789'); 2. int4 3. ------ 4. 789 5. (1 row) 6. openGauss=# select int4(99.9); 7. int4 8. ------ 9. 99 10. (1 row) - float4(in)

描述:将传入参数转换为float4类型值并返回。支持的入参类型包括:bigint,duoble precision,int16, integer, numeric,smallint,text。

返回值类型:float4

示例:

``` 1. openGauss=# select float4('789'); 2. float4 3. -------- 4. 789 5. (1 row)

  1. openGauss=# select float4(99.9);
  2. float4

  3. 99.9
  4. (1 row) ```
  5. float8(in)

描述:将传入参数转换为float8类型值并返回。支持的入参类型包括:bigint,int16, integer, numeric,real,smallint,text。

返回值类型:float8

示例:

``` 1. openGauss=# select float8('789'); 2. float8 3. -------- 4. 789 5. (1 row)

  1. openGauss=# select float8(99.9);
  2. float8

  3. 99.9
  4. (1 row) ```
  5. int16(in)

描述:将传入参数转换为int16类型值并返回。支持的入参类型包括:bigint,boolean,double precision,integer,numeric,oid,real,smallint,tinyint。

返回值类型:int16

示例:

``` 1. openGauss=# select int16('789'); 2. int16 3. -------- 4. 789 5. (1 row)

  1. openGauss=# select int16(99.9);
  2. int16

  3. 99
  4. (1 row) ```
  5. numeric(in)

描述:将传入参数转换为numeric类型值并返回。支持的入参类型包括:bigint,boolean,double precision,int16,integer,money,real,smallint。

返回值类型:numeric

示例:

``` 1. openGauss=# select "numeric"('789'); 2. numeric 3. --------- 4. 789 5. (1 row)

  1. openGauss=# select "numeric"(99.9);
  2. numeric

  3. 99.9
  4. (1 row) ```
  5. oid(in)

描述:将传入参数转换为oid类型值并返回。支持的入参类型包括:bigint,int16。

返回值类型:oid - radians(dp)

描述:把角度转为弧度。

返回值类型:double precision

示例:

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

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

返回值类型:double precision

示例:

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

描述:x和y的乘积。仅支持multiply(x double precision, y text)和multiply(x text, y double precision)的两种场景。

返回值类型:double precision

示例:

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

描述:自然对数。

返回值类型:dp or numeric,不考虑隐式类型转换的情况下与输入相同。

示例:

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

描述:以10为底的对数。

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

示例:

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

描述:以b为底的对数。

返回值类型:numeric

示例:

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

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

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

示例:

1. openGauss=# SELECT mod(9,4); 2. mod 3. ----- 4. 1 5. (1 row)

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

描述:“π”常量。

返回值类型:double precision

示例:

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

描述:a的b次幂。

返回值类型:double precision

示例:

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

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

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

示例:

``` 1. openGauss=# SELECT round(42.4); 2. round 3. ------- 4. 42 5. (1 row)

  1. openGauss=# SELECT round(42.6);
  2. round

  3. 43
  4. (1 row) ```
  5. round(v numeric, s int)

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

返回值类型:numeric

示例:

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

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

返回值类型:void

示例:

``` 1. openGauss=# SELECT setseed(0.54823); 2. setseed 3. ---------

  1. (1 row) ```
  2. sign(x)

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

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

示例:

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

描述:正弦。

返回值类型:double precision

示例:

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

描述:平方根。

返回值类型:dp or numeric,不考虑隐式类型转换的情况下与输入相同。

示例:

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

描述:正切。

返回值类型:double precision

示例:

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

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

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

示例:

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

描述:截断为s位小数。

返回值类型:numeric

示例:

1. openGauss=# 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 - int1abs

描述:返回uint8类型数据的绝对值。

参数:tinyint

返回值类型:tinyint - int1and

描述:返回两个uint8类型数据按位与的结果。

参数:tinyint, tinyint

返回值类型:tinyint - int1cmp

描述:返回两个uint8类型数据比较的结果,若第一个参数大,则返回1;若第二个参数大,则返回-1;若相等,则返回0。

参数:tinyint, tinyint

返回值类型:integer - int1div

描述:返回两个uint8类型数据相除的结果,结果为float8类型。

参数:tinyint, tinyint

返回值类型:tinyint - int1eq

描述:比较两个uint8类型数据是否相等。

参数:tinyint, tinyint

返回值类型:boolean - int1ge

描述:判断两个uint8类型数据是否第一个参数大于等于第二个参数。

参数:tinyint, tinyint

返回值类型:boolean - int1gt

描述:无符号1字节整数做大于运算。

参数:tinyint, tinyint

返回值类型:boolean - int1larger

描述:无符号1字节整数求最大值。

参数:tinyint, tinyint

返回值类型:tinyint - int1le

描述:无符号1字节整数做小于等于运算。

参数:tinyint, tinyint

返回值类型:boolean - int1lt

描述:无符号1字节整数做小于运算。

参数:tinyint, tinyint

返回值类型:boolean - int1smaller

描述:无符号1字节整数求最小算。

参数:tinyint, tinyint

返回值类型:tinyint - int1inc

描述:无符号1字节整数加一。

参数:tinyint

返回值类型:tinyint - int1mi

描述:无符号一字节整数做差运算。

参数:tinyint, tinyint

返回值类型:tinyint - int1mod

描述:无符号一字节整数做取余运算。

参数:tinyint, tinyint

返回值类型:tinyint - int1mul

描述:无符号一字节整数做乘法运算。

参数:tinyint, tinyint

返回值类型:tinyint - int1ne

描述:无符号一字节整数不等于运算。

参数:tinyint, tinyint

返回值类型:boolean - int1pl

描述:无符号一字节整数加法。

参数:tinyint, tinyint

返回值类型:tinyint - int1um

描述:无符号一字节数去相反数并返回有符号二字节整数。

参数:tinyint

返回值类型:smallint - int1xor

描述:无符号一字节整数异或操作。

参数:tinyint, tinyint

返回值类型:tinyint - cash_div_int1

描述:对money类型进行除法运算。

参数:money, tinyint

返回值类型:money - cash_mul_int1

描述:对money类型进行乘法运算。

参数:money, tinyint

返回值类型:money - int1not

描述:无符号一字节整数二进制位翻转。

参数:tinyint

返回值类型:tinyint - int1or

描述:无符号一字节整数或运算。

参数:tinyint, tinyint

返回值类型:tinyint - int1shl

描述:无符号一字节整数左移指定位数。

参数:tinyint, integer

返回值类型:tinyint - width_bucket(op numeric, b1 numeric, b2 numeric, count int)

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

返回值类型:int

示例:

1. openGauss=# 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. openGauss=# SELECT width_bucket(5.35, 0.024, 10.06, 5); 2. width_bucket 3. -------------- 4. 3 5. (1 row)