函数

openGauss常用的函数如下:

数学函数

  • abs(x)

描述:绝对值。

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

示例:

1. openGauss=# SELECT abs(-17.4); 2. abs 3. ------ 4. 17.4 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) - degrees(dp)

描述:把弧度转为角度。

返回值类型:double precision

示例:

1. openGauss=# SELECT degrees(0.5); 2. degrees 3. ------------------ 4. 28.6478897565412 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) - 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) - 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) - 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) - sign(x)

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

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

示例:

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

描述:平方根。

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

示例:

1. openGauss=# SELECT sqrt(2.0); 2. sqrt 3. ------------------- 4. 1.414213562373095 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)

三角函数列表

  • 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) - 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) - sin(x)

描述:正弦。

返回值类型:double precision

示例:

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

描述:正切。

返回值类型:double precision

示例:

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

字符串函数和操作符

  • string || string

描述:连接字符串。

返回值类型:text

示例:

1. openGauss=# SELECT 'MPP'||'DB' AS RESULT; 2. result 3. -------- 4. MPPDB 5. (1 row) - bit_length(string)

描述:字符串的位数。

返回值类型:int

示例:

1. openGauss=# SELECT bit_length('world'); 2. bit_length 3. ------------ 4. 40 5. (1 row) - convert(string bytea, src_encoding name, dest_encoding name)

描述:以dest_encoding指定的目标编码方式转化字符串bytea。src_encoding指定源编码方式,在该编码下,string必须是合法的。

返回值类型:bytea

示例:

1. openGauss=# SELECT convert('text_in_utf8', 'UTF8', 'GBK'); 2. convert 3. ---------------------------- 4. \x746578745f696e5f75746638 5. (1 row) - lower(string)

描述:把字符串转化为小写。

返回值类型:varchar

示例:

1. openGauss=# SELECT lower('TOM'); 2. lower 3. ------- 4. tom 5. (1 row) - octet_length(string)

描述:字符串中的字节数。

返回值类型:int

示例:

1. openGauss=# SELECT octet_length('jose'); 2. octet_length 3. -------------- 4. 4 5. (1 row) - overlay(string placing string FROM int [for int])

描述:替换子字符串。FROM int表示从第一个string的第几个字符开始替换,for int表示替换第一个string的字符数目。

返回值类型:text

示例:

1. openGauss=# SELECT overlay('hello' placing 'world' from 2 for 3 ); 2. overlay 3. --------- 4. hworldo 5. (1 row) - position(substring in string)

描述:指定子字符串的位置。字符串区分大小写。

返回值类型:int,字符串不存在时返回0。

示例:

1. openGauss=# SELECT position('ing' in 'string'); 2. position 3. ---------- 4. 4 5. (1 row) - substring(string [from int] [for int])

描述:截取子字符串,from int表示从第几个字符开始截取,for int表示截取几个字节。

返回值类型:text

示例:

1. openGauss=# SELECT substring('Thomas' from 2 for 3); 2. substring 3. ----------- 4. hom 5. (1 row) - substring(string from pattern)

描述:截取匹配POSIX正则表达式的子字符串。如果没有匹配它返回空值,否则返回文本中匹配模式的那部分。

返回值类型:text

示例:

1. openGauss=# SELECT substring('Thomas' from '...$'); 2. substring 3. ----------- 4. mas 5. (1 row) 6. openGauss=# SELECT substring('foobar' from 'o(.)b'); 7. result 8. -------- 9. o 10. (1 row) 11. openGauss=# SELECT substring('foobar' from '(o(.)b)'); 12. result 13. -------- 14. oob 15. (1 row) - trim([leading |trailing |both] [characters] from string)

描述:从字符串string的开头、结尾或两边删除只包含characters中字符(缺省是一个空白)的最长的字符串。

返回值类型:varchar

示例:

1. openGauss=# SELECT trim(BOTH 'x' FROM 'xTomxx'); 2. btrim 3. ------- 4. Tom 5. (1 row)

1. openGauss=# SELECT trim(LEADING 'x' FROM 'xTomxx'); 2. ltrim 3. ------- 4. Tomxx 5. (1 row)

1. openGauss=# SELECT trim(TRAILING 'x' FROM 'xTomxx'); 2. rtrim 3. ------- 4. xTom 5. (1 row) - upper(string)

描述:把字符串转化为大写。

返回值类型:varchar

示例:

1. openGauss=# SELECT upper('tom'); 2. upper 3. ------- 4. TOM 5. (1 row) - ascii(string)

描述:参数string的第一个字符的ASCII码。

返回值类型:integer

示例:

1. openGauss=# SELECT ascii('xyz'); 2. ascii 3. ------- 4. 120 5. (1 row) - btrim(string text [, characters text])

描述:从string开头和结尾删除只包含characters中字符(缺省是空白)的最长字符串。

返回值类型:text

示例:

1. openGauss=# SELECT btrim('sring' , 'ing'); 2. btrim 3. ------- 4. sr 5. (1 row) - chr(integer)

描述:给出ASCII码的字符。

返回值类型:varchar

示例:

1. openGauss=# SELECT chr(65); 2. chr 3. ----- 4. A 5. (1 row) - convert(string bytea, src_encoding name, dest_encoding name)

描述:以dest_encoding指定的目标编码方式转化字符串bytea。src_encoding指定源编码方式,在该编码下,string必须是合法的。

返回值类型:bytea

示例:

1. openGauss=# SELECT convert('text_in_utf8', 'UTF8', 'GBK'); 2. convert 3. ---------------------------- 4. \x746578745f696e5f75746638 5. (1 row) - initcap(string)

描述:将字符串中的每个单词的首字母转化为大写,其他字母转化为小写。

返回值类型:text

示例:

1. openGauss=# SELECT initcap('hi THOMAS'); 2. initcap 3. ----------- 4. Hi Thomas 5. (1 row) - length(string)

描述:获取参数string中字符的数目。

返回值类型:integer

示例:

1. openGauss=# SELECT length('abcd'); 2. length 3. -------- 4. 4 5. (1 row) - lpad(string text, length int [, fill text])

描述:通过填充字符fill(缺省时为空白),把string填充为length长度。如果string已经比length长则将其尾部截断。

返回值类型:text

示例:

1. openGauss=# SELECT lpad('hi', 5, 'xyza'); 2. lpad 3. ------- 4. xyzhi 5. (1 row) - ltrim(string [, characters])

描述:从字符串string的开头删除只包含characters中字符(缺省是一个空白)的最长的字符串。

返回值类型:varchar

示例:

1. openGauss=# SELECT ltrim('xxxxTRIM','x'); 2. ltrim 3. ------- 4. TRIM 5. (1 row) - md5(string)

描述:将string使用MD5加密,并以16进制数作为返回值。

函数 - 图1 说明: MD5加密算法安全性低,存在安全风险,不建议使用。

返回值类型:text

示例:

1. openGauss=# SELECT md5('ABC'); 2. md5 3. ---------------------------------- 4. 902fbdd2b1df0c4f70b4a5d23525e932 5. (1 row) - repeat(string text, number int )

描述:将string重复number次。

返回值类型:text。

示例:

1. openGauss=# SELECT repeat('Pg', 4); 2. repeat 3. ---------- 4. PgPgPgPg 5. (1 row) - replace(string text, from text, to text)

描述:把字符串string里出现地所有子字符串from的内容替换成子字符串to的内容。

返回值类型:text

示例:

1. openGauss=# SELECT replace('abcdefabcdef', 'cd', 'XXX'); 2. replace 3. ---------------- 4. abXXXefabXXXef 5. (1 row) - rpad(string text, length int [, fill text])

描述:使用填充字符fill(缺省时为空白),把string填充到length长度。如果string已经比length长则将其从尾部截断。

返回值类型:text

示例:

1. openGauss=# SELECT rpad('hi', 5, 'xy'); 2. rpad 3. ------- 4. hixyx 5. (1 row) - rtrim(string text [, characters text])

描述:从字符串string的结尾删除只包含characters中字符(缺省是个空白)的最长的字符串。

返回值类型:text

示例:

1. openGauss=# SELECT rtrim('trimxxxx', 'x'); 2. rtrim 3. ------- 4. trim 5. (1 row) - split_part(string text, delimiter text, field int)

描述:根据delimiter分隔string返回生成的第field个子字符串(从出现第一个delimiter的text为基础)。

返回值类型:text

示例:

1. openGauss=# SELECT split_part('abc~@~def~@~ghi', '~@~', 2); 2. split_part 3. ------------ 4. def 5. (1 row) - strpos(string, substring)

描述:指定的子字符串的位置。和position(substring in string)一样,不过参数顺序相反。

返回值类型:int

示例:

1. openGauss=# SELECT strpos('source', 'rc'); 2. strpos 3. -------- 4. 4 5. (1 row) - to_hex(number int or bigint)

描述:把number转换成十六进制表现形式。

返回值类型:text

示例:

1. openGauss=# SELECT to_hex(2147483647); 2. to_hex 3. ---------- 4. 7fffffff 5. (1 row) - translate(string text, from text, to text)

描述:把在string中包含的任何匹配from中字符的字符转化为对应的在to中的字符。如果from比to长,删掉在from中出现的额外的字符。

返回值类型:text

示例:

1. openGauss=# SELECT translate('12345', '143', 'ax'); 2. translate 3. ----------- 4. a2x5 5. (1 row)

类型转换相关函数

  • to_char(timestamp, text)

描述:将时间戳类型的值转换为指定格式的字符串。

返回值类型:text

示例:

1. openGauss=# SELECT to_char(current_timestamp, 'HH12:MI:SS'); 2. to_char 3. ---------- 4. 10:55:59 5. (1 row) - to_char(interval, text)

描述:将时间间隔类型的值转换为指定格式的字符串。

返回值类型:text

示例:

1. openGauss=# SELECT to_char(interval '15h 2m 12s', 'HH24:MI:SS'); 2. to_char 3. ---------- 4. 15:02:12 5. (1 row) - to_char(int, text)

描述:将整数类型的值转换为指定格式的字符串。

返回值类型:text

示例:

1. openGauss=# SELECT to_char(125, '999'); 2. to_char 3. --------- 4. 125 5. (1 row) - to_char(double precision/real, text)

描述:将浮点类型的值转换为指定格式的字符串。

返回值类型:text

示例:

1. openGauss=# SELECT to_char(125.8::real, '999D99'); 2. to_char 3. --------- 4. 125.80 5. (1 row) - to_char(numeric, text)

描述:将数字类型的值转换为指定格式的字符串。

返回值类型:text

示例:

1. openGauss=# SELECT to_char(-125.8, '999D99S'); 2. to_char 3. --------- 4. 125.80- 5. (1 row) - to_date(text, text)

描述:将字符串类型的值转换为指定格式的日期。

返回值类型:timestamp without time zone

示例:

1. openGauss=# SELECT to_date('05 Dec 2000', 'DD Mon YYYY'); 2. to_date 3. --------------------- 4. 2000-12-05 00:00:00 5. (1 row) - to_number(text, text)

描述:将字符串类型的值转换为指定格式的数字。

返回值类型:numeric

示例:

1. openGauss=# SELECT to_number('12,454.8-', '99G999D9S'); 2. to_number 3. ----------- 4. -12454.8 5. (1 row) - to_timestamp(text, text)

描述:将字符串类型的值转换为指定格式的时间戳。

返回值类型:timestamp

示例:

1. openGauss=# SELECT to_timestamp('05 Dec 2000', 'DD Mon YYYY'); 2. to_timestamp 3. --------------------- 4. 2000-12-05 00:00:00 5. (1 row) - to_timestamp(double precision)

描述:把UNIX纪元转换成时间戳。

返回值类型:timestamp with time zone

示例:

1. openGauss=# SELECT to_timestamp(1284352323); 2. to_timestamp 3. ------------------------ 4. 2010-09-13 12:32:03+08 5. (1 row)