加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql——字符串函数

发布时间:2020-12-13 16:11:42 所属栏目:百科 来源:网络整理
导读:字符串函数: postgresql中的字符串函数有:计算字符串长度函数、字符串合并函数、字符串替换函数、字符串比较函数、查找指定字符串位置函数等。 1、计算字符串字符数和字符串长度的函数:char_length(str)和length(str) char_length(str)返回值为字符串str
字符串函数:

postgresql中的字符串函数有:计算字符串长度函数、字符串合并函数、字符串替换函数、字符串比较函数、查找指定字符串位置函数等。



1、计算字符串字符数和字符串长度的函数:char_length(str)和length(str)

char_length(str)返回值为字符串str所包含的字符个数。一个多字节字符算作一个单字符。


例子:使用char_length函数计算字符串字符个数,如:


testdb=# select char_length('date'),char_length('zhang');

?char_length | char_length?

-------------+-------------

? ? ? ? ? ?4 |? ? ? ? ? ?5

(1 row)


length(str)返回值为字符串的字节长度,使用utf8编码字符集时,一个汉字是3字节,一个数字或字母算一个字节。

testdb=# select length('date'),length('zhang');

?length | length?

--------+--------

? ? ? 4 |? ? ? 5

(1 row)


注意:length函数的计算结果和char_length函数相同,因为英文字符的个数和所占字节相同,一个字符占一个字节。


2、合并字符串函数:concat(s1,s2,)、concat_ws(x,s1,)

? concat(s1,)返回结果为连接参数产生的字符串。任何一个参数为null,返回值就为null。如果所有参数为非二进制字符串,那么结果为非二进制字符串。如果自变量中含有任一二进制字符串,那么结果就为一个二进制字符串。

concat_ws(x,)x是与其他参数的分隔符。


例子:使用concat函数连接字符串,如下:

testdb=# select concat('postgresql','9.6'),concat('postgresql',null,'testdb');

? ? concat? ? ?|? ? ? concat? ? ??

---------------+------------------

?postgresql9.6 | postgresqltestdb

(1 row)



例子:使用concat_ws函数连接带分隔符的字符串,如:

testdb=# select concat_ws('_','postgresql',concat_ws('**','testdb');

? ?concat_ws? ? |? ? ?concat_ws? ? ??

----------------+--------------------

?postgresql_9.6 | postgresql**testdb

(1 row)



3、获取指定长度的字符串的函数:left(s,n)和right(s,n)

left(s,n)返回字符串s开始的最左边n个字符。


例子:使用left函数返回字符串中左边开始的5个字符,如

testdb=# select? left('football',5);

?left??

-------

?footb

(1 row)


right(s,n)返回字符串s最右边个字符


例子:使用right函数返回字符串中右边的字符,如:

testdb=# select? right('football',4);

?right?

-------

?ball

(1 row)


4、填充字符串的函数:lpad(s1,len,s2)和rpad(s1,s2)

lpad(s1,s2)返回字符串s1,其左边由字符s2填充,填充长度为len,加入s1的长度大于len,则返回值被缩短至len字符。


例子:使用lpad函数对字符串进行填充操作,如下:


testdb=# select lpad('hello',4,'??'),lpad('hello',10,'??');

?lpad |? ? lpad? ??

------+------------

?hell | ?????hello

(1 row)


rpad(s1,s2)返回字符串s1,其右边被字符串s2填补至len字符长度。假如字符串的长度大于len,则返回值被缩短到与len字符相同的长度。


例子:使用rpad函数对字符串进行填充操作,如:

testdb=# select rpad('hello','?'),rpad('hello','?');

?rpad |? ? rpad? ??

------+------------

?hell | hello?????

(1 row)


5、删除空格的函数:ltrim(s)、rtrim(s)和trim(s)

ltrim(s)返回字符串s,字符串左侧空格字符被删除。


例子:使用ltrim函数删除字符串左边的空格,如:

testdb=# select '( book )',concat('(',ltrim(' book '),')');

??column? | concat??

----------+---------

?( book ) | (book )

(1 row)


rtrim(s)返回字符串s,字符串右侧空格字符被删除。


例子:使用rtrim函数删除字符串右边的空格,如:


testdb=# select '( book )',rtrim(' book '),')');

??column? | concat??

----------+---------

?( book ) | ( book)

(1 row)


trim(s)删除字符串s两侧的空格。


例子:使用trim函数删除指定字符串两端的空格,如:

testdb=# select '( book )',trim(' book '),')');

??column? | concat?

----------+--------

?( book ) | (book)

(1 row)


6、删除指定字符串的函数:trim(s1 from s)


trim(s1 from s)删除字符串s中两端所有的子字符串s1.s1为可选项,在未指定情况下删除空格。


例子:使用trim(s1 from s)函数删除字符串中两端指定的字符,如:

testdb=# select trim('zh' from 'zhanghellzhnihao');

? ? ?btrim? ? ??

----------------

?anghellzhnihao

(1 row)


7、重复生成字符串的函数:repeat(s,n)

repeat(s,n)返回一个由重复的字符串s组成的字符串,n表示重复生成的次数。若n<=0,则返回一个空字符串;若s或n为null,则返回null。


例子:使用repeat函数重复生成相同的字符串,如:

testdb=# select repeat('zhang',3);

? ? ?repeat? ? ??

-----------------

?zhangzhangzhang

(1 row)


8、替换函数:replace(s,s2)

replace(s,s2)使用字符串s2替代字符串s中所有字符串s1.


例子:使用replace函数进行字符串替代操作。

testdb=# select replace('www.baidu.com','w','z');

? ? replace? ??

---------------

?zzz.baidu.com

(1 row)


9、获取子串的函数:substring(s,n,len)

substring(s,len)表示从字符串s返回一个长度为len的子字符串,起始于位置n。也可能对n使用一个负值。假若这样,则子字符串的位置起始于字符串结尾的n的字符,即倒数第n个字符。

testdb=# select substring('zhanghello',5) as a1,substring('zhanghello',5,3) as a2,substring('lunch',-3) as a3;

? ?a1? ?| a2? |? a3? ?

--------+-----+-------

?ghello | ghe | lunch

(1 row)


注意:如果对len使用的是一个小于1的值,那么结果始终为整个字符串。


10、匹配子串开始位置的函数:position(str1? in? str)

position(str1? in? str)函数的作用是返回子字符串str1在字符串str中的开始位置。


例子:使用position函数查找字符串中指定str1在字符串的开始位置,如:

testdb=#? select position('ball' in 'football');

?position?

----------

? ? ? ? 5

(1 row)


11、字符串逆序函数:reverse(s)

reverse(s)将字符串s反转,返回的字符串的顺序和s字符顺序相反。


例子:使用reverse函数反转字符串,如:

testdb=# select reverse('abc');

?reverse?

---------

?cba

(1 row)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读