使用函数可以极大的提高用户对数据库的管理效率,函数表示输入参数表示一个具有特定关系的值,下面这篇文章主要给大家介绍了关于Postgresql常用函数及使用方法的相关资料,需要的朋友可以参考下
前言
写在前面,当我们在用postgresql数据库进行数据取数时,难免会遇到一些数据处理上的场景,本文会针对一些常用的内置函数进行总结。
-- 查看postgresql的版本(以下任一语句都可以实现) select version(); show server_version;
1.格式转换
1.1 格式转换符显示转换
利用双冒号可以直接进行格式转换,语法如下:
字段名或数值::数据类型
例子如下:
-- 将文本'123'转为int8类型 SELECT '123' :: int8 num; -- 将文本类型字段t1转为int8类型 SELECT t1 :: int8 from temp;
1.2 利用数据类型显示转换
利用数据类型进行转换,语法如下:
数据类型数值
例子:
-- 将文本'123'转为int8类型 SELECT int8'123' num;
1.3 格式转换函数显示转换
利用数据转换函数cast进行转换,语法如下:
cast(字段名或数值 as 数据类型)
例子:
-- 将文本'123'转为int8类型 SELECT cast('123' as int4) num; -- 将文本字段t1转为int类型 SELECT CAST( t1 AS INT ) t1_c FROM TEMP;
1.4 转换案例
-- 文本转整数 SELECT CAST ( '123' AS int4 ); -- 文本转浮点数字 SELECT CAST ( '123.34' AS DECIMAL ); SELECT CAST ( '123.34' AS NUMERIC ); -- 数字转文本 SELECT CAST ( 123 AS VARCHAR );--可变字符串 SELECT CAST ( - 123 AS CHAR ( 2 ) );-- 固定字符串,进行截断,将-123转为'-1' SELECT CAST ( - 123 AS CHAR ( 6 ) );-- 固定字符串,进行空格填充,将-123转为'-123 ' SELECT CAST ( 124.94 AS TEXT );--可变字符串,将124.94转为'124.94' SELECT to_char( 124.94, '999D9' );--将124.94转为'124.9',遵循四舍五入 SELECT to_char( 124.94, 'FM999.99' );--将124.94转为'124.94' SELECT to_char( - 124.94, 'FM9999999.99' );--将-124.94转为'-124.94' SELECT to_char( - 124.94, 'FM9999999.990' );--将-124.94转为'-124.940' SELECT to_char( 124, '00000' );--左端用零补齐凑够5位,将124转为'00124' SELECT to_char( 124, '99999' );--左端用空格补齐凑够5位,将124转为' 124' SELECT to_char( - 124.945, 'FM999' );--只显示整数部分,遵循四舍五入 -- 时间戳(timestamp)转日期(date) SELECT CAST ( now( ) AS DATE );--普通日期模式 -- 时间戳(timestamp)转文本 SELECT CAST ( now( ) AS TEXT );--不指定输出格式 SELECT to_char( now( ), 'yyyy-mm-dd' );--指定输出格式; -- 文本转日期(date) SELECT to_date( '2012-01-01', 'yyyy-mm-dd' ); -- 文本转时间戳(TIMESTAMP) SELECT to_timestamp( '2012-01-01 12:02:01', 'yyyy-mm-dd HH24:MI:SS' );
2.数学计算
2.1 数学运算操作符
操作符 | 描述 | 例子 | 结果 |
---|---|---|---|
+ | 加 | 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 |
& | 按位与 | 91 & 15 | 11 |
| | 按位或 | 32 | 3 |
# | 按位异或 | 17 # 5 | 20 |
~ | 按位求反 | ~1 | -2 |
<< | 按位左移 | 1 << 4 | 16 |
>> | 按位右移 | 8 >> 2 | 2 |
2.2 数学运算函数
函数 | 返回类型 | 描述 | 例子 | 结果 |
---|---|---|---|---|
abs(x) | 和输入相同 | 绝对值 | abs(-12.43) | 12.43 |
cbrt(dp) | double | 立方根 | cbrt(27.0) | 3 |
ceil(dp or numeric) | 和输入相同 | 不小于参数的最近的整数 | ceil(-42.8) | -42 |
ceiling(dp or numeric) | 和输入相同 | 不小于参数的最近的整数(ceil的别名) | ceiling(-95.3) | -95 |
degrees(dp) | dp | 把弧度转为角度 | degrees(0.5) | 28.6478897565412 |
div(y numeric, x numeric) | numeric | y/x的整数商 | div(9,4)<360> | 2 |
exp(dp or numeric) | 和输入相同 | 指数 | exp(1.0) | 2.71828182845905 |
floor(dp or numeric) | 和输入相同 | 不大于参数的最近的整数 | floor(-42.8) | -43 |
ln(dp or numeric) | 和输入相同 | 自然对数 | ln(2.0) | 0.693147180559945 |
log(dp or numeric) | 和输入相同 | 以10为底的对数 | log(100.0) | 2 |
log10(dp or numeric) | 和输入相同 | 以10为底的对数 | log10(100.0) | 2 |
log(b numeric, x numeric) | numeric | 以b为底的对数 | log(2.0, 64.0) | 6.0000000000 |
mod(y, x) | 和参数类型相同 | y/x的余数 | mod(9,4) | 1 |
pi() | dp | “π”常数 | pi() | 3.14159265358979 |
power(a dp, b dp) | dp | 求a的b次幂 | power(9.0, 3.0) | 729 |
power(a numeric, b numeric) | numeric | 求a的b次幂 | power(9.0, 3.0) | 729 |
radians(dp) | dp | 把角度转为弧度 | radians(45.0) | 0.785398163397448 |
round(dp or numeric) | 和输入相同 | 圆整为最接近的整数 | round(42.4) | 42 |
round(v numeric, s int) | numeric | 圆整为s位小数数字 | round(42.4382, 2) | 42.44 |
scale(numeric) | integer | 参数的精度(小数点后的位数) | scale(8.41) | 2 |
sign(dp or numeric) | 和输入相同 | 参数的符号(-1, 0, +1) | sign(-8.4) | -1 |
sqrt(dp or numeric) | 和输入相同 | 平方根 | sqrt(2.0) | 1.4142135623731 |
trunc(dp or numeric) | 和输入相同 | 截断(向零靠近) | trunc(42.8) | 42 |
trunc(v numeric, s int) | numeric | 截断为s位小数位置的数字 | trunc(42.4382, 2) | 42.43 |
3.逻辑计算
3.1 逻辑操作符
postgresql中的逻辑操作符,有以下三种:
AND
OR
NOT
3.2 比较操作符
操作符 | 描述 |
---|---|
< | 小于 |
> | 大于 |
<= | 小于等于 |
>= | 大于等于 |
= | 等于 |
<> or != | 不等于 |
!=操作符在分析器阶段被转换成<>
3.3 比较谓词
谓词 | 描述 |
---|---|
a BETWEEN x AND y | 在x和y之间 |
a NOT BETWEEN x AND y | 不在x和y之间 |
a BETWEEN SYMMETRIC x AND y | 在对比较值排序后位于x和y之间 |
a NOT BETWEEN SYMMETRIC x AND y | 在对比较值排序后不位于x和y之间 |
a IS DISTINCT FROM b | 不等于,空值被当做一个普通值 |
a IS NOT DISTINCT FROM b | 等于,空值被当做一个普通值 |
expression IS NULL | 是空值 |
expression IS NOT NULL | 不是空值 |
expression ISNULL | 是空值(非标准语法) |
expression NOTNULL | 不是空值(非标准语法) |
boolean_expression IS TRUE | 为真 |
boolean_expression IS NOT TRUE | 为假或未知 |
boolean_expression IS FALSE | 为假 |
boolean_expression IS NOT FALSE | 为真或者未知 |
boolean_expression IS UNKNOWN | 值为未知 |
boolean_expression IS NOT UNKNOWN | 为真或者为假 |
3.4 比较函数
函数 | 描述 | 例子 | 例子结果 |
---|---|---|---|
num_nonnulls(VARIADIC “any”) | 返回非空参数的数量 | num_nonnulls(0, NULL, 1 ,2 ,3) | 4 |
num_nulls(VARIADIC “any”) | 返回空参数的数量 | num_nulls(0, NULL, 1 ,2 ,3) | 1 |
4.字符串及相关匹配函数
函数 | 返回类型 | 描述 | 例子 | 结果 |
---|---|---|---|---|
string || string | text | 串接 | ‘Hello’ || ‘Word’ | ‘HelloWord’ |
string || non-string or non-string || string | text | 使用一个非字符串输入的串接 | 'Value: ’ || 42 | Value: 42 |
bit_length(string) | int | 串中的位数 | bit_length(‘Hello’) | 40 |
char_length(string) or character_length(string) | int | 串中字符数 | char_length(‘Hello’) | 4 |
lower(string) | text | 将字符串转换为小写形式 | lower(‘Hello’) | hello |
overlay(string placing string from int [for int]) | text | 替换子串,for后面是指替换的位数 | overlay(‘Hexxx,word’ placing ‘llo’ from 3 for 4) | Helloword |
position(substring in string) | int | 定位指定子串位置,可利用值是否大于0来判断是否包含子串 | position(‘lo’ in ‘hello’) | 4 |
substring(string [from int] [for int]) | text | 提取子串 | substring(‘hello’ from 1 for 3) | hel |
substring(string from pattern) | text | 提取匹配POSIX正则表达式的子串 | substring(‘hello’ from ‘^…’) | hel |
substr(string, from [, count]) | text | 提取子串 | substr(‘Hello’, 1, 3) | hel |
trim([leading | trailing | both] [characters] from string) | text | 从string的开头、结尾或者两端(both是默认值)移除只包含characters(默认是一个空格)中字符的最长字符串 | trim(both ‘Hes’ from ‘sHehelloeHs’) | hello |
trim([leading | trailing | both] [from] string [, characters] ) | text | trim()的非标准版本 | trim(both from ‘hhHellohh’, ‘h’) 或trim(‘hhHellohh’, ‘h’) | Tom |
upper(string) | text | 将字符串转换成大写形式 | upper(‘hello’) | HELLO |
concat(str “any” [, str “any” [, …] ]) | text | 串接所有参数的文本表示。NULL 参数被忽略。 | concat(‘abcde’, 2, NULL, 22) | abcde222 |
concat_ws(sep text, str “any” [, str “any” [, …] ]) | text | 将除了第一个参数外的其他参数用分隔符串接在一起。第一个参数被用作分隔符字符串。NULL 参数被忽略。 | concat_ws(‘,’, ‘abcde’, 2, NULL, 22) | abcde,2,22 |
left(str text, n int) | text | 返回字符串中的前n个字符。当n为负时,将返回除了最后|n|个字符之外的所有字符。 | left(‘abcde’, 2) | ab |
length(string) | int | string中的字符数 | length(‘hello’) | 5 |
length(string bytea, encoding name ) | int | string在给定编码中的字符数。string必须在这个编码中有效。 | length(‘hello’, ‘UTF8’) | 5 |
lpad(string text, length int [, fill text]) | text | 将string通过前置字符fill(默认是一个空格)填充到长度length。如果string已经长于length,则它被(从右边)截断。 | lpad(‘hi’, 5, ‘ab’) | abahi |
ltrim(string text [, characters text]) | text | 从string的开头删除最长的只包含characters(默认是一个空格)的串 | ltrim(‘zzzytest’, ‘xyz’) | test |
regexp_match(string text, pattern text [, flags text]) | text[] | 返回一个POSIX正则表达式与string的第一个匹配得到的子串。 | regexp_match(‘foobarbequetarz’, ‘(foo)(bar)’) | 一行:{foo,bar} |
regexp_matches(string text, pattern text [, flags text]) |
评论 0 |
发表评论 取消回复