|
TOP
|
|
postgresql整理(二)
|
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 |
| translate(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 |
把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 |
把字串转换成numeric |
to_number('12,454.8-', '99G999D9S') |
日期/时间函数:
| 函数 |
返回类型 |
描述 |
例子 |
结果 |
| age(timestamp, timestamp) |
interval |
减去参数,生成一个使用年、月的"符号化"的结果 |
age('2001-04-10', timestamp '1957-06-13') |
43 years 9 mons 27 days |
| age(timestamp) |
interval |
从current_date减去得到的数值 |
age(timestamp '1957-06-13') |
43 years 8 mons 3 days |
| current_date |
date |
今天的日期 |
|
|
| current_time |
time |
现在的时间 |
|
|
| current_timestamp |
timestamp |
日期和时间 |
|
|
| date_part(text, timestamp) |
double |
获取子域(等效于extract) |
date_part('hour', timestamp '2001-02-16 20:38:40') |
20 |
| date_part(text, interval) |
double |
获取子域(等效于extract) |
date_part('month', interval '2 years 3 months') |
3 |
| date_trunc(text, timestamp) |
timestamp |
截断成指定的精度 |
date_trunc('hour', timestamp '2001-02-16 20:38:40') |
2001-02-16 20:00:00+00 |
| extract(field from timestamp) |
double |
获取子域 |
extract(hour from timestamp '2001-02-16 20:38:40') |
20 |
| extract(field from interval) |
double |
获取子域 |
extract(month from interval '2 years 3 months') |
3 |
| localtime |
time |
今日的时间 |
|
|
| localtimestamp |
timestamp |
日期和时间 |
|
|
| now() |
timestamp |
当前的日期和时间(等效于 current_timestamp) |
|
|
| timeofday() |
text |
当前日期和时间 |
|
|
EXTRACT、date_part函数支持的field:
| 域 |
描述 |
例子 |
结果 |
| DAY |
(月分)里的日期域(1-31) |
EXTRACT(DAY from TIMESTAMP '2001-02-16 20:38:40'); |
16 |
| DOW |
每周的星期号(0-6;星期天是0) (仅用于timestamp) |
EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40'); |
5 |
| DOY |
一年的第几天(1 -365/366) (仅用于 timestamp) |
EXTRACT(DOY from TIMESTAMP '2001-02-16 20:38:40'); |
47 |
| HOUR |
小时域(0-23) |
EXTRACT(HOUR from TIMESTAMP '2001-02-16 20:38:40'); |
20 |
| MICROSECONDS |
秒域,包括小数部分,乘以 1,000,000。 |
EXTRACT(MICROSECONDS from TIME '17:12:28.5'); |
28500000 |
| MILLISECONDS |
秒域,包括小数部分,乘以 1000。 |
EXTRACT(MILLISECONDS from TIME '17:12:28.5'); |
28500 |
| MINUTE |
分钟域(0-59) |
EXTRACT(MINUTE from TIMESTAMP '2001-02-16 20:38:40'); |
38 |
| MONTH |
对于timestamp数值,它是一年里的月份数(1-12);对于interval数值,它是月的数目,然后对12取模(0-11) |
EXTRACT(MONTH from TIMESTAMP '2001-02-16 20:38:40'); |
2 |
| QUARTER |
该天所在的该年的季度(1-4)(仅用于 timestamp) |
EXTRACT(QUARTER from TIMESTAMP '2001-02-16 20:38:40'); |
1 |
| SECOND |
秒域,包括小数部分(0-59[1]) |
EX |
|