PostgreSQL常用字符串函数与示例说明

coalesce

coalesce主要用来处理空,它返回第1个不为空的值,可以接受整型或者字符串,但是不能混有。

select coalesce('a','0') as r1,coalesce(1,2) as r2,coalesce(null,2,1) as r3,coalesce(null,NULL,'first') as r4;

PostgreSQL coalesce函数

字符串位置(position strpos)

返回子字符串在字符串中的位置,有点像Java中的index

select position('456' in '123456789') as r1,strpos('123456789', '678') as r2;

PostgreSQL 字符串位置

字符串长度与大小写转换

select length('hello world') as len, upper('Hello World') as up, lower('Hello World') as lo;

PostgreSQL字符串长度与大小写转换

去掉空格(trim ltrim rtrim)

select trim(' Hello World ') as t,
rtrim(' Hello World ') as r,
ltrim(' Hello World ') as l;

PostgreSQL去掉空格

字符串连接(concat)

select concat('Hello', ' ', 'World');
select concat(ip, ' ', port) as id from user;

PostgreSQL从9.1开始提供了||操作符,可以用来代替concat函数。

select ip || ' ' || port as id from user;

字符串替换

简单替换(replace)

-- 第1个参数是源字符串,第2个参数是需要的替换的old,第3个参数是替换之后的new
select replace('啊哈, 娘子', '娘子', '相公') as r;

PostgreSQL字符串替换

替换指定位置长度(overlay)

overlay(string placing substring from start [for length]): 用另一个字符串替换字符串的一部分。

-- 结果:123中文6干嘛orld
-- 将第7位开始的2位替换为了干嘛,其他不变
select overlay(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值