PostgreSQL常用函数和操作符:
一、逻辑操作符:
常用的逻辑操作符有:AND、OR和NOT。其语义与其它编程语言中的逻辑操作符完全相同。
二、比较操作符:
下面是PostgreSQL中提供的比较操作符列表:
| 操作符 | 描述 |
| < | 小于 |
| > | 大于 |
| <= | 小于或等于 |
| >= | 大于或等于 |
| = | 等于 |
| != | 不等于 |
比较操作符可以用于所有可以比较的数据类型。所有比较操作符都是双目操作符,且返回boolean类型。除了比较操作符以外,我们还可以使用BETWEEN语句,如:
a BETWEEN x AND y 等效于 a >= x AND a <= y
a NOT BETWEEN x AND y 等效于 a < x OR a > y
三、 数学函数和操作符:
下面是PostgreSQL中提供的数学操作符列表:
| 操作符 | 描述 | 例子 | 结果 |
| + | 加 | 2 + 3 | 5 |
| - | 减 | 2 - 3 | -1 |
| * | 乘 | 2 * 3 | 6 |
| / | 除 | 4 / 2 | 2 |
| % | 模 | 5 % 4 | 1 |
| ^ | 幂 | 2.0 ^ 3.0 | 8 |
| |/ | 平方根 | |/ 25.0 | 5 |
| ||/ | 立方根 | ||/ 27.0 | 3 |
| ! | 阶乘 | 5 ! | 120 |
| !! | 阶乘 | !! 5 | 120 |
| @ | 绝对值 | @ -5.0 | 5 |
| & | 按位AND | 91 & 15 | 11 |
| | | 按位OR | 32 | 3 | 35 |
| # | 按位XOR | 17 # 5 | 20 |
| ~ | 按位NOT | ~1 | -2 |
| << | 按位左移 | 1 << 4 | 16 |
| >> | 按位右移 | 8 >> 2 | 2 |
按位操作符只能用于整数类型,而其它的操作符可以用于全部数值数据类型。按位操作符还可以用于位串类型bit和bit varying,
下面是PostgreSQL中提供的数学函数列表,需要说明的是,这些函数中有许多都存在多种形式,区别只是参数类型不同。除非特别指明,任何特定形式的函数都返回和它的参数相同的数据类型。
| 函数 | 返回类型 | 描述 | 例子 | 结果 |
| abs(x) | 绝对值 | abs(-17.4) | 17.4 | |
| cbrt(double) | 立方根 | cbrt(27.0) | 3 | |
| ceil(double/numeric) | 不小于参数的最小的整数 | ceil(-42.8) | -42 | |
| degrees(double) | 把弧度转为角度 | degrees(0.5) | 28.6478897565412 | |
| exp(double/numeric) | 自然指数 | exp(1.0) | 2.71828182845905 | |
| floor(double/numeric) | 不大于参数的最大整数 | floor(-42.8) | -43 | |
| ln(double/numeric) | 自然对数 | ln(2.0) | 0.693147180559945 | |
| log(double/numeric) | 10为底的对数 | log(100.0) | 2 | |
| log(b numeric,x numeric) | numeric指定底数的对数 | log(2.0, 64.0) | 6.0000000000 | |
| mod(y, x) | 取余数 | mod(9,4) | 1 | |
| pi() | double | "π"常量 | pi() | 3.14159265358979 |
| power(a double, b double) | double | 求a的b次幂 | power(9.0, 3.0) | 729 |
| power(a numeric, b numeric) | numeric | 求a的b次幂 | power(9.0, 3.0) | 729 |
| radians(double) | double | 把角度转为弧度 | radians(45.0) | 0.785398163397448 |
| random() | double | 0.0到1.0之间的随机数值 | random() | |
| round(double/numeric) | 圆整为最接近的整数 | round(42.4) | 42 | |
| round(v numeric, s int) | numeric | 圆整为s位小数数字 | round(42.438,2) | 42.44 |
| sign(double/numeric) | 参数的符号(-1,0,+1) | sign(-8.4) | -1 | |
| sqrt(double/numeric) | 平方根 | sqrt(2.0) | 1.4142135623731 | |
| trunc(double/numeric) | 截断(向零靠近) | trunc(42.8) | 42 | |
| trunc(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 || string | text | 字串连接 | 'Post' || 'greSQL' | PostgreSQL |
| bit_length(string) | int | 字串里二进制位的个数 | bit_length('jose') | 32 |
| char_length(string) | int | 字串中的字符个数 | char_length('jose') | 4 |
| convert(string using conversion_name) | text | 使用指定的转换名字改变编码。 | convert('PostgreSQL' using iso_8859_1_to_utf8) | 'PostgreSQL' |
| lower(string) | text | 把字串转化为小写 | lower('TOM') | tom |
| octet_length(string) | int | 字串中的字节数 | octet_length('jose') | 4 |
| overlay(string placing string from int [for int]) | text | 替换子字串 | overlay('Txxxxas' placing 'hom' from 2 for 4) | Thomas |
| position(substring in string) | int | 指定的子字串的位置 | position('om' in 'Thomas') | 3 |
| substring(string [from int] [for int]) | text | 抽取子字串 | substring('Thomas' from 2 for 3) | hom |
| substring(string from pattern) | text | 抽取匹配 POSIX 正则表达式的子字串 | substring('Thomas' from '...$') | mas |
| substring(string from pattern for escape) | text | 抽取匹配SQL正则表达式的子字串 | substring('Thomas' from '%#"o_a#"_' for '#') | oma |
| trim([leading | trailing | both] [characters] from string) | text | 从字串string的开头/结尾/两边/ 删除只包含characters(缺省是一个空白)的最长的字串 | trim(both 'x' from 'xTomxx') | Tom |
| upper(string) | text | 把字串转化为大写。 | upper('tom') | TOM |
| ascii(text) | int | 参数第一个字符的ASCII码 | ascii('x') | 120 |
| btrim(string text [, characters text]) | text | 从string开头和结尾删除只包含在characters里(缺省是空白)的字符的最长字串 | btrim('xyxtrimyyx','xy') | trim |
| chr(int) | text | 给出ASCII码的字符 | chr(65) | A |
| convert(string text, [src_encoding name,] dest_encoding name) | text | 把字串转换为dest_encoding | convert( 'text_in_utf8', 'UTF8', 'LATIN1') | 以ISO 8859-1编码表示的text_in_utf8 |
| initcap(text) | text | 把每个单词的第一个子母转为大写,其它的保留小写。单词是一系列字母数字组成的字符,用非字母数字分隔。 | initcap('hi thomas') | Hi Thomas |
| length(string text) | int | string中字符的数目 | length('jose') | 4 |
| lpad(string text, length int [, fill text]) | text | 通过填充字符fill(缺省时为空白),把string填充为长度length。 如果string已经比length长则将其截断(在右边)。 | lpad('hi', 5, 'xy') | xyxhi |
| ltrim(string text [, characters text]) | text | 从字串string的开头删除只包含characters(缺省是一个空白)的最长的字串。 | ltrim('zzzytrim','xyz') | trim |
| md5(string text) | text | 计算给出string的MD5散列,以十六进制返回结果。 | md5('abc') | |
| repeat(string text, number int) | text | 重复string number次。 | repeat('Pg', 4) | PgPgPgPg |
| replace(string text, from text, to text) | text | 把字串string里出现地所有子字串from替换成子字串to。 | replace('abcdefabcdef', 'cd', 'XX') | abXXefabXXef |
| rpad(string text, length int [, fill text]) | text | 通过填充字符fill(缺省时为空白),把string填充为长度length。如果string已经比length长则将其截断。 | rpad('hi', 5, 'xy') | hixyx |
| rtrim(string text [, character text]) | text | 从字串string的结尾删除只包含character(缺省是个空白)的最长的字 | rtrim('trimxxxx','x') | trim |
| split_part(string text, delimiter text, field int) | text | 根据delimiter分隔string返回生成的第field个子字串(1 Base)。 | split_part('abc~@~def~@~ghi', '~@~', 2) | def |
| strpos(string, substring) | text | 声明的子字串的位置。 | strpos('high','ig') | 2 |
| substr(string, from [, count]) | text | 抽取子字串。 | substr('alphabet', 3, 2) | ph |
| to_ascii(text [, encoding]) | text | 把text从其它编码转换为ASCII。 | to_ascii('Karel') | Karel |
| to_hex(number int/bigint) | text | 把number转换成其对应地十六进制表现形式。 | to_hex(9223372036854775807) | 7fffffffffffffff |
| translate(string text, from text, to text) | text | 把在string中包含的任何匹配from中的字符的字符转化为对应的在to中的字符。 | translate('12345', '14', 'ax') | a23x5 |
五、位串函数和操作符:
对于类型bit和bit varying,除了常用的比较操作符之外,还可以使用以下列表中由PostgreSQL提供的位串函数和操作符,其中&、|和#的位串操作数必须等长。在移位的时候,保留原始的位串的的长度。
| 操作符 | 描述 | 例子 | 结果 |
| || | 连接 | B'10001' || B'011' | 10001011 |
| & | 按位AND | B'10001' & B'01101' | 00001 |
| | | 按位OR | B'10001' | B'01101' | 11101 |
| # | 按位XOR | B'10001' # B'01101' | 11100 |
| ~ | 按位NOT | ~ B'10001' | 01110 |
| << | 按位左移 | B'10001' << 3 | 01000 |
| >> | 按位右移 | B'10001' >> 2 | 00100 |
除了以上列表中提及的操作符之外,位串还可以使用字符串函数:length, bit_length, octet_length, position, substring。此外,我们还可以在整数和bit之间来回转换,如:
MyTest=# SELECT 44::bit(10);
bit
------------
0000101100
(1 row)
MyTest=# SELECT 44::bit(3);
bit
-----
100
(1 row)
MyTest=# SELECT cast(-44 as bit(12));
bit
--------------
111111010100
(1 row)
MyTest=# SELECT '1110'::bit(4)::integer;
int4
------
14
(1 row)
注意:如果只是转换为"bit",意思是转换成bit(1),因此只会转换成整数的最低位。
PostgreSQL的时间/日期函数使用
PostgreSQL的常用时间函数使用整理如下:
一、获取系统时间函数
1.1 获取当前完整时间
select now();
david=# select now();
now
-------------------------------
2013-04-12 15:39:40.399711+08
(1 row)
david=#
current_timestamp 同 now() 函数等效。
david=# select current_timestamp;
now
-------------------------------
2013-04-12 15:40:22.398709+08
(1 row)
david=#
1.2 获取当前日期
select current_date;
david=# select current_date;
date
------------
2013-04-12
(1 row)
david=#
1.3 获取当前时间
select current_time;
david=# select current_time;
timetz
--------------------
15:43:31.101726+08
(1 row)
david=#
二、时间的计算
david=# select now();
now
-------------------------------
2013-04-12 15:47:13.244721+08
(1 row)
david=#
2.1 两年后
david=# select now() + interval '2 years';
?column?
-------------------------------
2015-04-12 15:49:03.168851+08
(1 row)
david=# select now() + interval '2 year';
?column?
-------------------------------
2015-04-12 15:49:12.378727+08
(1 row)
david=# select now() + interval '2 y';
?column?
------------------------------
2015-04-12 15:49:25.46986+08
(1 row)
david=# select now() + interval '2 Y';
?column?
-------------------------------
2015-04-12 15:49:28.410853+08
(1 row)
david=# select now() + interval '2Y';
?column?
-------------------------------
2015-04-12 15:49:31.122831+08
(1 row)
david=#
2.2 一个月后
david=# select now() + interval '1 month';
?column?
------------------------------
2013-05-12 15:51:22.24373+08
(1 row)
david=# select now() + interval 'one month';
ERROR: invalid input syntax for type interval: "one month"
LINE 1: select now() + interval 'one month';
^
david=#
2.3 三周前
david=# select now() - interval '3 week';
?column?
-------------------------------
2013-03-22 16:00:04.203735+08
(1 row)
david=#
2.4 十分钟后
david=# select now() + '10 min';
?column?
-------------------------------
2013-04-12 16:12:47.445744+08
(1 row)
david=#
说明:
interval 可以不写,其值可以是:
| Abbreviation | Meaning |
| Y | Years |
| M | Months (in the date part) |
| W | Weeks |
| D | Days |
| H | Hours |
| M | Minutes (in the time part) |
| S | Seconds |
2.5 计算两个时间差
使用 age(timestamp, timestamp)
david=# select age(now(), timestamp '1989-02-05');
age
----------------------------------------
24 years 2 mons 7 days 17:05:49.119848
(1 row)
david=#
david=# select age(timestamp '2007-09-15');
age
------------------------
5 years 6 mons 27 days
(1 row)
david=#
三、时间字段的截取
在开发过程中,经常要取日期的年,月,日,小时等值,PostgreSQL 提供一个非常便利的EXTRACT函数。
EXTRACT(field FROM source)
field 表示取的时间对象,source 表示取的日期来源,类型为 timestamp、time 或 interval。
3.1 取年份
david=# select extract(year from now());
date_part
-----------
2013
(1 row)
david=#
3.2 取月份
david=# select extract(month from now());
date_part
-----------
4
(1 row)
david=#
david=# select extract(day from timestamp '2013-04-13');
date_part
-----------
13
(1 row)
david=#
david=# SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
date_part
-----------
40
(1 row)
david=#
3.3 查看今天是一年中的第几天
david=# select extract(doy from now());
date_part
-----------
102
(1 row)
david=#
3.4 查看现在距1970-01-01 00:00:00 UTC 的秒数
david=# select extract(epoch from now());
date_part
------------------
1365755907.94474
(1 row)
david=#
3.5 把epoch 值转换回时间戳
david=# SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1369755555 * INTERVAL '1 second';
?column?
------------------------
2013-05-28 23:39:15+08
(1 row)
david=#
以上是基本的PG时间/日期函数使用,可满足一般的开发运维应用。
该博客中提供的所有信息均源自PostgreSQL官方文档。
