函数
openGauss常用的函数如下:
数学函数
abs(x)
描述:绝对值。
返回值类型:和输入相同。
示例:
openGauss=# SELECT abs(-17.4); abs ------ 17.4 (1 row)
cbrt(dp)
描述:立方根。
返回值类型:double precision
示例:
openGauss=# SELECT cbrt(27.0); cbrt ------ 3 (1 row)
ceil(x)
描述:不小于参数的最小的整数。
返回值类型:整数。
示例:
openGauss=# SELECT ceil(-42.8); ceil ------ -42 (1 row)
degrees(dp)
描述:把弧度转为角度。
返回值类型:double precision
示例:
openGauss=# SELECT degrees(0.5); degrees ------------------ 28.6478897565412 (1 row)
exp(x)
描述:自然指数。
返回值类型:dp or numeric,不考虑隐式类型转换的情况下与输入相同。
示例:
openGauss=# SELECT exp(1.0); exp -------------------- 2.7182818284590452 (1 row)
floor(x)
描述:不大于参数的最大整数。
返回值类型:与输入相同。
示例:
openGauss=# SELECT floor(-42.8); floor ------- -43 (1 row)
ln(x)
描述:自然对数。
返回值类型:dp or numeric,不考虑隐式类型转换的情况下与输入相同。
示例:
openGauss=# SELECT ln(2.0); ln ------------------- .6931471805599453 (1 row)
log(x)
描述:以10为底的对数。
返回值类型:与输入相同。
示例:
openGauss=# SELECT log(100.0); log -------------------- 2.0000000000000000 (1 row)
特性补充:在B库中log函数返回自然对数
示例:
openGauss=# SELECT log(100.0); log -------------------- 4.605170185988092 (1 row)
log(b numeric, x numeric)
描述:以b为底的对数。
返回值类型:numeric
示例:
openGauss=# SELECT log(2.0, 64.0); log -------------------- 6.0000000000000000 (1 row)
mod(x,y)
描述:x/y的余数(模)。如果x是0,则返回0。
返回值类型:与参数类型相同。
示例:
openGauss=# SELECT mod(9,4); mod ----- 1 (1 row)
openGauss=# SELECT mod(9,0); mod ----- 9 (1 row)
pi()
描述:“π”常量。
返回值类型:double precision
示例:
openGauss=# SELECT pi(); pi ------------------ 3.14159265358979 (1 row)
power(a double precision, b double precision)
描述:a的b次幂。
返回值类型:double precision
示例:
openGauss=# SELECT power(9.0, 3.0); power ---------------------- 729.0000000000000000 (1 row)
radians(dp)
描述:把角度转为弧度。
返回值类型:double precision
示例:
openGauss=# SELECT radians(45.0); radians ------------------ .785398163397448 (1 row)
random()
描述:0.0到1.0之间的随机数。
返回值类型:double precision
示例:
openGauss=# SELECT random(); random ------------------ .824823560658842 (1 row)
round(x)
描述:离输入参数最近的整数。
返回值类型:与输入相同(double precision或者numeric类型)。
示例:
openGauss=# SELECT round(42.4); round ------- 42 (1 row) openGauss=# SELECT round(42.6); round ------- 43 (1 row)
round(v numeric, s int)
描述:保留小数点后s位,s后一位进行四舍五入。
返回值类型:numeric
示例:
openGauss=# SELECT round(42.4382, 2); round ------- 42.44 (1 row)
sign(x)
描述:输出此参数的符号。
返回值类型:-1表示负数,0表示0,1表示正数。
示例:
openGauss=# SELECT sign(-8.4); sign ------ -1 (1 row)
sqrt(x)
描述:平方根。
返回值类型:dp or numeric,不考虑隐式类型转换的情况下与输入相同。
示例:
openGauss=# SELECT sqrt(2.0); sqrt ------------------- 1.414213562373095 (1 row)
trunc(x)
描述:截断(取整数部分)。
返回值类型:与输入相同。
示例:
openGauss=# SELECT trunc(42.8); trunc ------- 42 (1 row)
trunc(v numeric, s int4)
描述:截断为s位小数(trunc只支持截断不超过int4类型最大值的位数)。
返回值类型:numeric
示例:
openGauss=# SELECT trunc(42.4382, 2); trunc ------- 42.43 (1 row)
三角函数列表
acos(x)
描述:反余弦。
返回值类型:double precision
示例:
openGauss=# SELECT acos(-1); acos ------------------ 3.14159265358979 (1 row)
asin(x)
描述:反正弦。
返回值类型:double precision
示例:
openGauss=# SELECT asin(0.5); asin ------------------ .523598775598299 (1 row)
atan(x)
描述:反正切。
返回值类型:double precision
示例:
openGauss=# SELECT atan(1); atan ------------------ .785398163397448 (1 row)
atan2(y, x)
描述:y/x的反正切。
返回值类型:double precision
示例:
openGauss=# SELECT atan2(2, 1); atan2 ------------------ 1.10714871779409 (1 row)
cos(x)
描述:余弦。
返回值类型:double precision
示例:
openGauss=# SELECT cos(-3.1415927); cos ------------------- -.999999999999999 (1 row)
cot(x)
描述:余切。
返回值类型:double precision
示例:
openGauss=# SELECT cot(1); cot ------------------ .642092615934331 (1 row)
sin(x)
描述:正弦。
返回值类型:double precision
示例:
openGauss=# SELECT sin(1.57079); sin ------------------ .999999999979986 (1 row)
tan(x)
描述:正切。
返回值类型:double precision
示例:
openGauss=# SELECT tan(20); tan ------------------ 2.23716094422474 (1 row)
字符串函数和操作符
string || string
描述:连接字符串。
返回值类型:text
示例:
openGauss=# SELECT 'MPP'||'DB' AS RESULT; result -------- MPPDB (1 row)
bit_length(string)
描述:字符串的位数。
返回值类型:int
示例:
openGauss=# SELECT bit_length('world'); bit_length ------------ 40 (1 row)
convert(string bytea, src_encoding name, dest_encoding name)
描述:以dest_encoding指定的目标编码方式转化字符串bytea。src_encoding指定源编码方式,在该编码下,string必须是合法的。
返回值类型:bytea
示例:
openGauss=# SELECT convert('text_in_utf8', 'UTF8', 'GBK'); convert ---------------------------- \x746578745f696e5f75746638 (1 row)
lower(string)
描述:把字符串转化为小写。
返回值类型:varchar
示例:
openGauss=# SELECT lower('TOM'); lower ------- tom (1 row)
nls_lower(string, string)
描述:将字符串中的全部字母转化为小写,第二个参数用于指定排序方式。
返回值类型:text
示例:
openGauss=# SELECT nls_lower('TOM','NLS_SORT = C'); nls_lower ----------- tom (1 row)
octet_length(string)
描述:字符串中的字节数。
返回值类型:int
示例:
openGauss=# SELECT octet_length('jose'); octet_length -------------- 4 (1 row)
overlay(string placing string FROM int [for int])
描述:替换子字符串。FROM int表示从第一个string的第几个字符开始替换,for int表示替换第一个string的字符数目。
返回值类型:text
示例:
openGauss=# SELECT overlay('hello' placing 'world' from 2 for 3 ); overlay --------- hworldo (1 row)
position(substring in string)
描述:指定子字符串的位置。字符串区分大小写。
返回值类型:int,字符串不存在时返回0。
示例:
openGauss=# SELECT position('ing' in 'string'); position ---------- 4 (1 row)
substring(string [from int] [for int])
描述:截取子字符串,from int表示从第几个字符开始截取,for int表示截取几个字节。
返回值类型:text
示例:
openGauss=# SELECT substring('Thomas' from 2 for 3); substring ----------- hom (1 row)
substring(string from pattern)
描述:截取匹配POSIX正则表达式的子字符串。如果没有匹配它返回空值,否则返回文本中匹配模式的那部分。
返回值类型:text
示例:
openGauss=# SELECT substring('Thomas' from '...$'); substring ----------- mas (1 row) openGauss=# SELECT substring('foobar' from 'o(.)b'); result -------- o (1 row) openGauss=# SELECT substring('foobar' from '(o(.)b)'); result -------- oob (1 row)
trim([leading |trailing |both] [characters] from string)
描述:从字符串string的开头、结尾或两边删除只包含characters中字符(缺省是一个空白)的最长的字符串。
返回值类型:varchar
示例:
openGauss=# SELECT trim(BOTH 'x' FROM 'xTomxx'); btrim ------- Tom (1 row)
openGauss=# SELECT trim(LEADING 'x' FROM 'xTomxx'); ltrim ------- Tomxx (1 row)
openGauss=# SELECT trim(TRAILING 'x' FROM 'xTomxx'); rtrim ------- xTom (1 row)
upper(string)
描述:把字符串转化为大写。
返回值类型:varchar
示例:
openGauss=# SELECT upper('tom'); upper ------- TOM (1 row)
ascii(string)
描述:参数string的第一个字符的ASCII码。
返回值类型:integer
示例:
openGauss=# SELECT ascii('xyz'); ascii ------- 120 (1 row)
btrim(string text [, characters text])
描述:从string开头和结尾删除只包含characters中字符(缺省是空白)的最长字符串。
返回值类型:text
示例:
openGauss=# SELECT btrim('sring' , 'ing'); btrim ------- sr (1 row)
chr(integer)
描述:给出ASCII码的字符。
返回值类型:varchar
示例:
openGauss=# SELECT chr(65); chr ----- A (1 row)
convert(string bytea, src_encoding name, dest_encoding name)
描述:以dest_encoding指定的目标编码方式转化字符串bytea。src_encoding指定源编码方式,在该编码下,string必须是合法的。
返回值类型:bytea
示例:
openGauss=# SELECT convert('text_in_utf8', 'UTF8', 'GBK'); convert ---------------------------- \x746578745f696e5f75746638 (1 row)
initcap(string)
描述:将字符串中的每个单词的首字母转化为大写,其他字母转化为小写。
返回值类型:text
示例:
openGauss=# SELECT initcap('hi THOMAS'); initcap ----------- Hi Thomas (1 row)
nls_initcap(string, string)
描述:将字符串中的每个单词的首字母转化为大写,其他字母转化为小写。但是请注意,第二个参数不会生效。
返回值类型:text
示例:
openGauss=# SELECT nls_initcap('hi THOMAS'); nls_initcap ----------- Hi Thomas (1 row)
length(string)
描述:获取参数string中字符的数目。
返回值类型:integer
示例:
openGauss=# SELECT length('abcd'); length -------- 4 (1 row)
lpad(string text, length int [, fill text])
描述:通过填充字符fill(缺省时为空白),把string填充为length长度。如果string已经比length长则将其尾部截断。
返回值类型:text
示例:
openGauss=# SELECT lpad('hi', 5, 'xyza'); lpad ------- xyzhi (1 row)
ltrim(string [, characters])
描述:从字符串string的开头删除只包含characters中字符(缺省是一个空白)的最长的字符串。
返回值类型:varchar
示例:
openGauss=# SELECT ltrim('xxxxTRIM','x'); ltrim ------- TRIM (1 row)
md5(string)
描述:将string使用MD5加密,并以16进制数作为返回值。
说明: MD5加密算法安全性低,存在安全风险,不建议使用。
返回值类型:text
示例:
openGauss=# SELECT md5('ABC'); md5 ---------------------------------- 902fbdd2b1df0c4f70b4a5d23525e932 (1 row)
repeat(string text, number int )
描述:将string重复number次。
返回值类型:text。
示例:
openGauss=# SELECT repeat('Pg', 4); repeat ---------- PgPgPgPg (1 row)
replace(string text, from text, to text)
描述:把字符串string里出现地所有子字符串from的内容替换成子字符串to的内容。
返回值类型:text
示例:
openGauss=# SELECT replace('abcdefabcdef', 'cd', 'XXX'); replace ---------------- abXXXefabXXXef (1 row)
rpad(string text, length int [, fill text])
描述:使用填充字符fill(缺省时为空白),把string填充到length长度。如果string已经比length长则将其从尾部截断。
返回值类型:text
示例:
openGauss=# SELECT rpad('hi', 5, 'xy'); rpad ------- hixyx (1 row)
rtrim(string text [, characters text])
描述:从字符串string的结尾删除只包含characters中字符(缺省是个空白)的最长的字符串。
返回值类型:text
示例:
openGauss=# SELECT rtrim('trimxxxx', 'x'); rtrim ------- trim (1 row)
split_part(string text, delimiter text, field int)
描述:根据delimiter分隔string返回生成的第field个子字符串(从出现第一个delimiter的text为基础)。
返回值类型:text
示例:
openGauss=# SELECT split_part('abc~@~def~@~ghi', '~@~', 2); split_part ------------ def (1 row)
strpos(string, substring)
描述:指定的子字符串的位置。和position(substring in string)一样,不过参数顺序相反。
返回值类型:int
示例:
openGauss=# SELECT strpos('source', 'rc'); strpos -------- 4 (1 row)
cast(x as y)
描述:类型转换函数,将x转换成y指定的类型。
返回值类型:type_name
示例:
openGauss=# SELECT cast('22-oct-1997' as timestamp); timestamp --------------------- 1997-10-22 00:00:00 (1 row)
CAST( expr AS type_name [ DEFAULT return_value ON CONVERSION ERROR ][, fmt [, 'nlsparam' ] ])
描述:类型转换函数,将x转换成y指定的类型,并且支持DEFAULT设置默认值、fmt指定入参的输入格式以及nlsparam参数,目前nlsparam参数仅支持English和American。
返回值类型:type_name
示例:
openGauss=# SELECT cast('12,454.8-' as numeric, '99G999D9S'); numeric ---------- -12454.8 (1 row)
openGauss=# SELECT cast('111111.111'+'1111' as numeric, '99G999D9S'); numeric ------------ 112222.111 (1 row)
openGauss=# SELECT cast('1e5'+'1111' as numeric, '999999.99'); numeric --------- 101111 (1 row)
openGauss=# SELECT cast('此参数错误' as numeric default 321456231 on conversion error, '999,999,999,999.99'); numeric ----------- 321456231 (1 row)
openGauss=# SELECT cast('12-sep-2014' as timestamp , 'DD-Mon-YYYY'); timestamp --------------------- 2014-09-12 00:00:00 (1 row)
openGauss=# SELECT cast ('此为错误输入' as timestamp DEFAULT '11-01-11 14:10:10.123000' ON CONVERSION ERROR,'DD-MM-RR HH24:MI:SS.FF'); timestamp ------------------------- 2011-11-01 14:10:10.123 (1 row)
openGauss=# SELECT cast ('01-Jan-03 14:10:10.123000' as timestamp DEFAULT '11-Jan-11 14:10:10.123000' ON CONVERSION ERROR,'DD-Mon-RR HH24:MI:SS.FF','NLS_DATE_LANGUAGE = American'); timestamp ------------------------- 2003-01-01 14:10:10.123 (1 row)
to_hex(number int or bigint)
描述:把number转换成十六进制表现形式。
返回值类型:text
示例:
openGauss=# SELECT to_hex(2147483647); to_hex ---------- 7fffffff (1 row)
translate(string text, from text, to text)
描述:把在string中包含的任何匹配from中字符的字符转化为对应的在to中的字符。如果from比to长,删掉在from中出现的额外的字符。
返回值类型:text
示例:
openGauss=# SELECT translate('12345', '143', 'ax'); translate ----------- a2x5 (1 row)
类型转换相关函数
to_char(timestamp, text)
描述:将时间戳类型的值转换为指定格式的字符串。
返回值类型:text
示例:
openGauss=# SELECT to_char(current_timestamp, 'HH12:MI:SS'); to_char ---------- 10:55:59 (1 row)
to_char(interval, text)
描述:将时间间隔类型的值转换为指定格式的字符串。
返回值类型:text
示例:
openGauss=# SELECT to_char(interval '15h 2m 12s', 'HH24:MI:SS'); to_char ---------- 15:02:12 (1 row)
to_char(int, text)
描述:将整数类型的值转换为指定格式的字符串。
返回值类型:text
示例:
openGauss=# SELECT to_char(125, '999'); to_char --------- 125 (1 row)
to_char(double precision/real, text)
描述:将浮点类型的值转换为指定格式的字符串。
返回值类型:text
示例:
openGauss=# SELECT to_char(125.8::real, '999D99'); to_char --------- 125.80 (1 row)
to_char(numeric, text)
描述:将数字类型的值转换为指定格式的字符串。
返回值类型:text
示例:
openGauss=# SELECT to_char(-125.8, '999D99S'); to_char --------- 125.80- (1 row)
to_date(text, text)
描述:将字符串类型的值转换为指定格式的日期。
返回值类型:timestamp without time zone
示例:
openGauss=# SELECT to_date('05 Dec 2000', 'DD Mon YYYY'); to_date --------------------- 2000-12-05 00:00:00 (1 row)
to_number( expr [ DEFAULT return_value ON CONVERSION ERROR ] [, fmt ])
描述:将expr按指定格式转换为一个NUMERIC类型的值,若expr部分转换失败,则会尝试转换关键字DEFAULT后的return_value(return_value的输入格式受fmt约束)。
类型转换格式(fmt)请参考表1。
fmt整数部分的长度必须大于expr整数部分的长度,若小于则会报错。fmt小数部分可以按需填写长度,输出结果会依照fmt的小数部分长度将expr进行截断。
expr和default后的return_value皆支持隐式转换。(注:NULL加任何值等于NULL)
支持科学计数法。
转换十六进制字符串为十进制数字时,最多支持16个字节的十六进制字符串转换为无符号数。
转换十六进制字符串为十进制数字时,格式字符串中不允许出现除'x'或'X'以外的其他字符,否则报错。
在behavior_compat_options的correct_to_number开关关闭的时候,如有分组,expr分组的个数要求和fmt的分组个数一样,否则报错。
当fmt的模式配置为数值位(模式为9)时,对于expr 中的非数值输入会进行忽略处理。
返回值类型:numeric
示例:
openGauss=# SELECT to_number('12,454.8-', '99G999D9S'); to_number ----------- -12454.8 (1 row)
openGauss=# SELECT to_number('1234.123','999999.99'); to_number ----------- 1234.12 (1 row)
openGauss=# SELECT to_number('111111.111'+'1111','999999.99'); to_number ----------- 112222.11 (1 row)
openGauss=# SELECT to_number('1e5'+'1111','999999.99'); to_number ----------- 101111 (1 row)
openGauss=# SELECT to_number('111111.111'+'1111'+NULL,'999999.99'); to_number ----------- (1 row)
openGauss=# SELECT to_number('此参数错误' default 321456231 on conversion error ,'999,999,999,999.99'); to_number ----------- 321456231 (1 row)
-- behavior_compat_options的correct_to_number开关关闭时不允许分组的数量不一样 openGauss=# select to_number('123.45' ,'999,999.999'); ERROR: invalid data. CONTEXT: referenced column: to_number openGauss=# set behavior_compat_options = 'correct_to_number'; SET openGauss=# select to_number('123.45' ,'999,999.999'); to_number ----------- 123.45 (1 row)
-- 当fmt的模式配置为数值位(模式为9)时,对于expr 中的非数值输入会进行忽略处理 openGauss=# select to_number('123,123','999999999'); to_number ----------- 123123 (1 row) openGauss=# select to_number('123.123','999999999'); to_number ----------- 123123 (1 row) openGauss=# select to_number('123S123','999999999'); to_number ----------- 123123 (1 row)
to_timestamp(double precision)
描述:把UNIX纪元转换成时间戳。
返回值类型:timestamp with time zone
示例:
openGauss=# SELECT to_timestamp(1284352323); to_timestamp ------------------------ 2010-09-13 12:32:03+08 (1 row)
to_timestamp(string [ DEFAULT return_value ON CONVERSION ERROR ] [ , fmt [, 'nlsparam' ] ])
描述:将字符串转换为时间戳。默认的输入格式为[DD-Mon-YYYY HH12:MI:SS.FF],是12小时制的AM。若输入值string不为默认的格式,则需要用户在fmt处描述自己的输入格式,若格式描述错误则报错。若string部分转换失败,则会尝试转换关键字DEFAULT后的return_value(return_value的输入格式受fmt约束)。若Mon处为Jan这样的英文缩写,则可以在nlsparam处设置月份的描述语言(目前仅支持American和English)。
- 如果输入的年份YYYY=0,系统报错。
- 如果输入的年份YYYY<0,在fmt中指定SYYYY,则正确输出公元前绝对值n的年份。
- fmt中的类似于MM的参数,不可大小写混用。
- fmt和string时间信息中的连接符可以替换为其他符号。例:SELECT to_timestamp('05*Dec^2000', 'DD Mon+YYYY');
- HH和HH12均为01-12小时格式,在fmt指定为HH和HH12的模式下且没明确指定pm的时间戳中,会将时间戳的第12小时按照第0小时处理。
返回值类型:timestamp without time zone
示例:
openGauss=# SHOW nls_timestamp_format; nls_timestamp_format ---------------------------- DD-Mon-YYYY HH:MI:SS.FF AM (1 row)
openGauss=# SELECT to_timestamp('12-sep-2014'); to_timestamp --------------------- 2014-09-12 00:00:00 (1 row)
openGauss=# SELECT to_timestamp ('01-Jan-2002 10:10:10.123000'); to_timestamp ------------------------- 2002-01-01 10:10:10.123 (1 row)
openGauss=# SELECT to_timestamp ('2002-01-01 10:10:10.123000','YYYY-MM-DD HH24:MI:SS.FF'); to_timestamp ------------------------- 2002-01-01 10:10:10.123 (1 row)
openGauss=# SELECT to_timestamp ('此为错误输入' DEFAULT '11-01-11 14:10:10.123000' ON CONVERSION ERROR,'DD-MM-RR HH24:MI:SS.FF'); to_timestamp ------------------------- 2011-01-11 14:10:10.123 (1 row)
openGauss=# SELECT to_timestamp ('01-Jan-03 14:10:10.123000' DEFAULT '11-Jan-11 14:10:10.123000' ON CONVERSION ERROR,'DD-Mon-RR HH24:MI:SS.FF','NLS_DATE_LANGUAGE = American'); to_timestamp ------------------------- 2003-01-01 14:10:10.123 (1 row)
openGauss=# SELECT to_timestamp('-1','SYYYY'); to_timestamp ------------------------ 0001-01-01 00:00:00 BC (1 row)
openGauss=# SELECT to_timestamp('05 Dec 2000', 'DD Mon YYYY'); to_timestamp --------------------- 2000-12-05 00:00:00 (1 row)
openGauss=# SELECT to_timestamp('05*Dec^2000', 'DD Mon+YYYY'); to_timestamp --------------------- 2000-12-05 00:00:00 (1 row)
-- HH或者HH12的模式下,不指定为PM时间时12点按照0点处理 openGauss=# select to_timestamp('2025-09-01 12:24:02','YYYY-MM-DD HH:MI:SS'); to_timestamp --------------------- 2025-09-01 00:24:02 (1 row) openGauss=# select to_timestamp('2025-09-01 12:24:02 pm','YYYY-MM-DD HH:MI:SS pm'); to_timestamp --------------------- 2025-09-01 12:24:02 (1 row)