pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。
许多数据类型都有可用于转换到其他相关类型的函数。此外还有一些特定于类型的函数。同时可通过运算符使用的函数仅以运算符的形式记载。
为 text 定义的一些函数也可用于 char() 和 varchar()。
对于 date_part 和 date_trunc 函数,参数可以是 `year'、`month'、`day'、`hour'、`minute' 和 `second',以及更专门的量 `decade'、`century'、`millenium'、`millisecond' 和 `microsecond'。date_part 还允许用 `dow' 返回星期几,用 `epoch' 返回自 1970 年以来的秒数(对 datetime),或用 'epoch' 返回总经过秒数(对 timespan)。
表 10.1. 数学函数
| 函数 | 返回值 | 描述 | 示例 |
|---|---|---|---|
| float(int) | float8 | 将整数转换为浮点数 | float(2) |
| float4(int) | float4 | 将整数转换为浮点数 | float4(2) |
| int | integer(float) | 将浮点数转换为整数 | integer(2.0) |
许多字符串函数可用于 text、varchar() 和 char() 类型。目前有些函数只能用于 text 类型。
表 10.2. 字符串函数
| 函数 | 返回值 | 描述 | 示例 |
|---|---|---|---|
| lower(text) | text | 将文本转换为小写 | lower('TOM') |
| lpad(text,int,text) | text | 在左侧将字符串填充到指定长度 | lpad('hi',4,'??') |
| ltrim(text,text) | text | 从文本左侧去除字符 | ltrim('xxxxtrim','x') |
| position(text,text) | text | 提取指定的子串 | position('high','ig') |
| rpad(text,int,text) | text | 在右侧将字符串填充到指定长度 | rpad('hi',4,'x') |
| rtrim(text,text) | text | 从文本右侧去除字符 | rtrim('trimxxxx','x') |
| substr(text,int[,int]) | text | 提取指定的子串 | substr('hi there',3,5) |
| upper(text) | text | 将文本转换为大写 | upper('tom') |
表 10.3. 日期/时间函数
| 函数 | 返回值 | 描述 | 示例 |
|---|---|---|---|
| isfinite(abstime) | bool | 如果这是一个有限时间则为 TRUE | isfinite('now'::abstime) |
| datetime(abstime) | datetime | 转换为 datetime | datetime('now'::abstime) |
| datetime(date) | datetime | 转换为 datetime | datetime('today'::date) |
| datetime(date,time) | datetime | 转换为 datetime | datetime('1998-02-24'::datetime, '23:07'::time); |
| age(datetime,datetime) | timespan | 保留月和年的区间 | age('now','1957-06-13'::datetime) |
| date_part(text,datetime) | float8 | 日期字段的指定部分 | date_part('dow','now'::datetime) |
| date_trunc(text,datetime) | datetime | 在指定单位处截断日期 | date_trunc('month','now'::abstime) |
| isfinite(datetime) | bool | 如果这是一个有限时间则为 TRUE | isfinite('now'::datetime) |
| abstime(datetime) | abstime | 转换为 abstime | abstime('now'::datetime) |
| timespan(reltime) | timespan | 转换为 timespan | timespan('4 hours'::reltime) |
| datetime(date,time) | datetime | 转换为 datetime | datetime('1998-02-25'::date,'06:41'::time) |
| date_part(text,timespan) | float8 | 时间字段的指定部分 | date_part('hour','4 hrs 3 mins'::timespan) |
| isfinite(timespan) | bool | 如果这是一个有限时间则为 TRUE | isfinite('4 hrs'::timespan) |
| reltime(timespan) | reltime | 转换为 reltime | reltime('4 hrs'::timespan) |
表 10.4. 几何函数
| 函数 | 返回值 | 描述 | 示例 |
|---|---|---|---|
| box(point,point) | box | 将点转换为方框 | box('(0,0)'::point,'(1,1)'::point) |
| area(box) | float8 | 方框的面积 | area('((0,0),(1,1))'::box) |
| isopen(path) | bool | 如果这是开放路径则为 TRUE | isopen('[(0,0),(1,1),(2,0)]'::path) |
| isclosed(path) | bool | 如果这是封闭路径则为 TRUE | isclosed('((0,0),(1,1),(2,0))'::path) |
| circle(point,float8) | circle | 转换为圆 | circle('(0,0)'::point,2.0) |
| polygon(npts,circle) | polygon | 转换为带 npts 个点的多边形 | polygon(12,'((0,0),2.0)'::circle) |
| center(circle) | float8 | 对象的中心 | center('((0,0),2.0)'::circle) |
| radius(circle) | float8 | 圆的半径 | radius('((0,0),2.0)'::circle) |
| diameter(circle) | float8 | 圆的直径 | diameter('((0,0),2.0)'::circle) |
| area(circle) | float8 | 圆的面积 | area('((0,0),2.0)'::circle) |
SQL92 定义了一些具有特定语法的函数。其中一些是用其他 Postgres 函数实现的。
表 10.5. SQL92 文本函数
| 函数 | 返回值 | 描述 | 示例 |
|---|---|---|---|
| position(text in text) | int4 | 提取指定的子串 | position('o' in 'Tom') |
| substring(text [from int] [for int]) | text | 提取指定的子串 | substring('Tom' from 2 for 2) |
| trim([leading|trailing|both] [text] from text) | text | 从文本中去除字符 | trim(both 'x' from 'xTomx') |
译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。