安徽省建设法治协会网站,杭州装修公司哪家好,wordpress flytag,重庆网上房地产网PostgreSQL常用函数
内置函数
PostgreSQL 内置函数也称为聚合函数#xff0c;用于对字符串或数字数据执行处理。
下面是所有通用 PostgreSQL 内置函数的列表#xff1a;
COUNT 函数#xff1a;用于计算数据库表中的行数。MAX 函数#xff1a;用于查询某一特定列中最大值…PostgreSQL常用函数
内置函数
PostgreSQL 内置函数也称为聚合函数用于对字符串或数字数据执行处理。
下面是所有通用 PostgreSQL 内置函数的列表
COUNT 函数用于计算数据库表中的行数。MAX 函数用于查询某一特定列中最大值。MIN 函数用于查询某一特定列中最小值。AVG 函数用于计算某一特定列中平均值。SUM 函数用于计算数字列所有值的总和。ARRAY 函数用于输入值(包括null)添加到数组中。Numeric 函数完整列出一个 SQL 中所需的操作数的函数。String 函数完整列出一个 SQL 中所需的操作字符的函数。
时间/日期函数
时间/日期操作符
下表演示了基本算术操作符的行为(,*, 等)
操作符例子结果date 2001-09-28 integer 7date 2001-10-05date 2001-09-28 interval 1 hourtimestamp 2001-09-28 01:00:00date 2001-09-28 time 03:00timestamp 2001-09-28 03:00:00interval 1 day interval 1 hourinterval 1 day 01:00:00timestamp 2001-09-28 01:00 interval 23 hourstimestamp 2001-09-29 00:00:00time 01:00 interval 3 hourstime 04:00:00-- interval 23 hoursinterval -23:00:00-date 2001-10-01 - date 2001-09-28integer 3 (days)-date 2001-10-01 - integer 7date 2001-09-24-date 2001-09-28 - interval 1 hourtimestamp 2001-09-27 23:00:00-time 05:00 - time 03:00interval 02:00:00-time 05:00 - interval 2 hourstime 03:00:00-timestamp 2001-09-28 23:00 - interval 23 hourstimestamp 2001-09-28 00:00:00-interval 1 day - interval 1 hourinterval 1 day -01:00:00-timestamp 2001-09-29 03:00 - timestamp 2001-09-27 12:00interval 1 day 15:00:00*900 * interval 1 secondinterval 00:15:00*21 * interval 1 dayinterval 21 days*double precision 3.5 * interval 1 hourinterval 03:30:00/interval 1 hour / double precision 1.5interval 00:40:00
日期/时间函数
函数返回类型描述例子结果age(timestamp, timestamp)interval减去参数后的符号化结果使用年和月不只是使用天age(timestamp 2001-04-10, timestamp 1957-06-13)43 years 9 mons 27 daysage(timestamp)interval从current_date减去参数后的结果在午夜age(timestamp 1957-06-13)43 years 8 mons 3 daysclock_timestamp()timestamp with time zone实时时钟的当前时间戳在语句执行时变化current_datedate当前的日期current_timetime with time zone当日时间current_timestamptimestamp with time zone当前事务开始时的时间戳date_part(text, timestamp)double precision获取子域(等效于extract)date_part(hour, timestamp 2001-02-16 20:38:40)20date_part(text, interval)double precision获取子域(等效于extract)date_part(month, interval 2 years 3 months)3date_trunc(text, timestamp)timestamp截断成指定的精度date_trunc(hour, timestamp 2001-02-16 20:38:40)2001-02-16 20:00:00date_trunc(text, interval)interval截取指定的精度date_trunc(hour, interval 2 days 3 hours 40 minutes)2 days 03:00:00extract(field from timestamp)double precision获取子域extract(hour from timestamp 2001-02-16 20:38:40)20extract(field from interval)double precision获取子域extract(month from interval 2 years 3 months)3isfinite(date)boolean测试是否为有穷日期(不是 /-无穷)isfinite(date 2001-02-16)trueisfinite(timestamp)boolean测试是否为有穷时间戳(不是 /-无穷)isfinite(timestamp 2001-02-16 21:28:30)trueisfinite(interval)boolean测试是否为有穷时间间隔isfinite(interval 4 hours)truejustify_days(interval)interval按照每月 30 天调整时间间隔justify_days(interval 35 days)1 mon 5 daysjustify_hours(interval)interval按照每天 24 小时调整时间间隔justify_hours(interval 27 hours)1 day 03:00:00justify_interval(interval)interval使用justify_days和justify_hours调整时间间隔的同时进行正负号调整justify_interval(interval 1 mon -1 hour)29 days 23:00:00localtimetime当日时间localtimestamptimestamp当前事务开始时的时间戳make_date(year int, month int, day int)date为年、月和日字段创建日期make_date(2013, 7, 15)2013-07-15make_interval(years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0)interval从年、月、周、天、小时、分钟和秒字段中创建间隔make_interval(days : 10)10 daysmake_time(hour int, min int, sec double precision)time从小时、分钟和秒字段中创建时间make_time(8, 15, 23.5)08:15:23.5make_timestamp(year int, month int, day int, hour int, min int, sec double precision)timestamp从年、月、日、小时、分钟和秒字段中创建时间戳make_timestamp(2013, 7, 15, 8, 15, 23.5)2013-07-15 08:15:23.5make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ])timestamp with time zone从年、月、日、小时、分钟和秒字段中创建带有时区的时间戳。 没有指定timezone时使用当前的时区。make_timestamptz(2013, 7, 15, 8, 15, 23.5)2013-07-15 08:15:23.501now()timestamp with time zone当前事务开始时的时间戳statement_timestamp()timestamp with time zone实时时钟的当前时间戳timeofday()text与clock_timestamp相同但结果是一个text 字符串transaction_timestamp()timestamp with time zone当前事务开始时的时间戳
字符串/日期相互格式化函数
函数返回类型描述例子to_char(timestamp, text)text把时间戳转换成字串to_char(current_timestamp, ‘HH12:MI:SS’)to_char(interval, text)text把时间间隔转为字串to_char(interval ‘15h 2m 12s’, ‘HH24:MI:SS’)to_char(int, text)text把整数转换成字串to_char(125, ‘999’)to_char(double precision, text)text把实数/双精度数转换成字串to_char(125.8::real, ‘999D9’)to_char(numeric, text)text把numeric转换成字串to_char(-125.8, ‘999D99S’)to_date(text, text)date把字串转换成日期to_date(‘05 Dec 2000’, ‘DD Mon YYYY’)to_timestamp(text, text)timestamp把字串转换成时间戳to_timestamp(‘05 Dec 2000’, ‘DD Mon YYYY’)to_timestamp(double)timestamp把UNIX纪元转换成时间戳to_timestamp(200120400)to_number(text, text)numeric把字串转换成numericto_number(‘12,454.8-’, ‘99G999D9S’)
日期/时间格式化的占位符
占位符描述HH一天的小时数(01-12)HH12一天的小时数(01-12)HH24一天的小时数(00-23)MI分钟(00-59)SS秒(00-59)MS毫秒(000-999)US微秒(000000-999999)AM正午标识(大写)Y,YYY带逗号的年(4和更多位)YYYY年(4和更多位)YYY年的后三位YY年的后两位Y年的最后一位MONTH全长大写月份名(空白填充为9字符)Month全长混合大小写月份名(空白填充为9字符)month全长小写月份名(空白填充为9字符)MON大写缩写月份名(3字符)Mon缩写混合大小写月份名(3字符)mon小写缩写月份名(3字符)MM月份号(01-12)DAY全长大写日期名(空白填充为9字符)Day全长混合大小写日期名(空白填充为9字符)day全长小写日期名(空白填充为9字符)DY缩写大写日期名(3字符)Dy缩写混合大小写日期名(3字符)dy缩写小写日期名(3字符)DDD一年里的日子(001-366)DD一个月里的日子(01-31)D一周里的日子(1-7周日是1)W一个月里的周数(1-5)(第一周从该月第一天开始)WW一年里的周数(1-53)(第一周从该年的第一天开始)
数学函数
下面是PostgreSQL中提供的数学函数列表需要说明的是这些函数中有许多都存在多种形式区别只是参数类型不同。除非特别指明任何特定形式的函数都返回和它的参数相同的数据类型。
函数返回类型描述例子结果abs(x)绝对值abs(-17.4)17.4cbrt(double)立方根cbrt(27.0)3ceil(double/numeric)不小于参数的最小的整数ceil(-42.8)-42degrees(double)把弧度转为角度degrees(0.5)28.6478897565412exp(double/numeric)自然指数exp(1.0)2.71828182845905floor(double/numeric)不大于参数的最大整数floor(-42.8)-43ln(double/numeric)自然对数ln(2.0)0.693147180559945log(double/numeric)10为底的对数log(100.0)2log(b numeric,x numeric)numeric指定底数的对数log(2.0, 64.0)6.0000000000mod(y, x)取余数mod(9,4)1pi()doubleπ常量pi()3.14159265358979power(a double, b double)double求a的b次幂power(9.0, 3.0)729power(a numeric, b numeric)numeric求a的b次幂power(9.0, 3.0)729radians(double)double把角度转为弧度radians(45.0)0.785398163397448random()double0.0到1.0之间的随机数值random()round(double/numeric)圆整为最接近的整数round(42.4)42round(v numeric, s int)numeric圆整为s位小数数字round(42.438,2)42.44sign(double/numeric)参数的符号(-1,0,1)sign(-8.4)-1sqrt(double/numeric)平方根sqrt(2.0)1.4142135623731trunc(double/numeric)截断(向零靠近)trunc(42.8)42trunc(v numeric, s int)numeric截断为s小数位置的数字trunc(42.438,2)42.43
三角函数
函数描述acos(x)反余弦asin(x)反正弦atan(x)反正切atan2(x, y)正切 y/x 的反函数cos(x)余弦cot(x)余切sin(x)正弦tan(x)正切
字符串函数和操作符
下面是 PostgreSQL 中提供的字符串操作符列表
函数返回类型描述例子结果string 丨丨 stringtext字串连接‘Post’ 丨丨 ‘greSQL’PostgreSQLbit_length(string)int字串里二进制位的个数bit_length(‘jose’)32char_length(string)int字串中的字符个数char_length(‘jose’)4convert(string using conversion_name)text使用指定的转换名字改变编码。convert(‘PostgreSQL’ using iso_8859_1_to_utf8)‘PostgreSQL’lower(string)text把字串转化为小写lower(‘TOM’)tomoctet_length(string)int字串中的字节数octet_length(‘jose’)4overlay(string placing string from int [for int])text替换子字串overlay(‘Txxxxas’ placing ‘hom’ from 2 for 4)Thomasposition(substring in string)int指定的子字串的位置position(‘om’ in ‘Thomas’)3substring(string [from int] [for int])text抽取子字串substring(‘Thomas’ from 2 for 3)homsubstring(string from pattern)text抽取匹配 POSIX 正则表达式的子字串substring(‘Thomas’ from ‘…$’)massubstring(string from pattern for escape)text抽取匹配SQL正则表达式的子字串substring(‘Thomas’ from ‘%#“o_a#”_’ for ‘#’)omatrim([leading丨trailing 丨 both] [characters] from string)text从字串string的开头/结尾/两边/ 删除只包含characters(默认是一个空白)的最长的字串trim(both ‘x’ from ‘xTomxx’)Tomupper(string)text把字串转化为大写。upper(‘tom’)TOMascii(text)int参数第一个字符的ASCII码ascii(‘x’)120btrim(string text [, characters text])text从string开头和结尾删除只包含在characters里(默认是空白)的字符的最长字串btrim(‘xyxtrimyyx’,‘xy’)trimchr(int)text给出ASCII码的字符chr(65)Aconvert(string text, [src_encoding name,] dest_encoding name)text把字串转换为dest_encodingconvert( ‘text_in_utf8’, ‘UTF8’, ‘LATIN1’)以ISO 8859-1编码表示的text_in_utf8initcap(text)text把每个单词的第一个子母转为大写其它的保留小写。单词是一系列字母数字组成的字符用非字母数字分隔。initcap(‘hi thomas’)Hi Thomaslength(string text)intstring中字符的数目length(‘jose’)4lpad(string text, length int [, fill text])text通过填充字符fill(默认为空白)把string填充为长度length。 如果string已经比length长则将其截断(在右边)。lpad(‘hi’, 5, ‘xy’)xyxhiltrim(string text [, characters text])text从字串string的开头删除只包含characters(默认是一个空白)的最长的字串。ltrim(‘zzzytrim’,‘xyz’)trimmd5(string text)text计算给出string的MD5散列以十六进制返回结果。md5(‘abc’)repeat(string text, number int)text重复string number次。repeat(‘Pg’, 4)PgPgPgPgreplace(string text, from text, to text)text把字串string里出现地所有子字串from替换成子字串to。replace(‘abcdefabcdef’, ‘cd’, ‘XX’)abXXefabXXefrpad(string text, length int [, fill text])text通过填充字符fill(默认为空白)把string填充为长度length。如果string已经比length长则将其截断。rpad(‘hi’, 5, ‘xy’)hixyxrtrim(string text [, character text])text从字串string的结尾删除只包含character(默认是个空白)的最长的字rtrim(‘trimxxxx’,‘x’)trimsplit_part(string text, delimiter text, field int)text根据delimiter分隔string返回生成的第field个子字串(1 Base)。split_part(‘abcdefghi’, ‘’, 2)defstrpos(string, substring)text声明的子字串的位置。strpos(‘high’,‘ig’)2substr(string, from [, count])text抽取子字串。substr(‘alphabet’, 3, 2)phto_ascii(text [, encoding])text把text从其它编码转换为ASCII。to_ascii(‘Karel’)Karelto_hex(number int/bigint)text把number转换成其对应地十六进制表现形式。to_hex(9223372036854775807)7ffffffffffffffftranslate(string text, from text, to text)text把在string中包含的任何匹配from中的字符的字符转化为对应的在to中的字符。translate(‘12345’, ‘14’, ‘ax’)a23x5
类型转换相关函数
函数返回类型描述实例to_char(timestamp, text)text将时间戳转换为字符串to_char(current_timestamp, ‘HH12:MI:SS’)to_char(interval, text)text将时间间隔转换为字符串to_char(interval ‘15h 2m 12s’, ‘HH24:MI:SS’)to_char(int, text)text整型转换为字符串to_char(125, ‘999’)to_char(double precision, text)text双精度转换为字符串to_char(125.8::real, ‘999D9’)to_char(numeric, text)text数字转换为字符串to_char(-125.8, ‘999D99S’)to_date(text, text)date字符串转换为日期to_date(‘05 Dec 2000’, ‘DD Mon YYYY’)to_number(text, text)numeric转换字符串为数字to_number(‘12,454.8-’, ‘99G999D9S’)to_timestamp(text, text)timestamp转换为指定的时间格式 time zone convert string to time stampto_timestamp(‘05 Dec 2000’, ‘DD Mon YYYY’)to_timestamp(double precision)timestamp把UNIX纪元转换成时间戳to_timestamp(1284352323)