参考:
https://www.postgresql.org/docs/current/functions-string.html
https://www.w3resource.com/PostgreSQL/overlay-function.php
D:\PostgreSQL\pg11.5\bin>psql -d postgres -U postgres -p 5901
用户 postgres 的口令:
psql (11.5)
输入 "help" 来获取帮助信息.
postgres=# select version();
version
------------------------------------------------------------
PostgreSQL 11.5, compiled by Visual C++ build 1914, 64-bit
(1 行记录)
postgres=# \x
扩展显示已打开。
postgres=# select 'Post' || 'greSQL';
-[ RECORD 1 ]--------
?column? | PostgreSQL
postgres=# \x
扩展显示已关闭。
postgres=# select 'Post' || 'greSQL';
?column?
------------
PostgreSQL
(1 行记录)
postgres=# select 'Value: ' || 42;
?column?
-----------
Value: 42 ----->>>返回的结果是text类型
(1 行记录)
postgres=# select concat('abcde', 2, NULL, 22);
concat
----------
abcde222
(1 行记录)
postgres=# select concat('abcde', 1, NULL, 2,null,3,4,null,5,null,6,null,7,null,8);
concat
---------------
abcde12345678 --->>>忽略NULL值,拼接一个字符串。
(1 行记录)
postgres=# select concat_ws(',', 'abcde', 2, NULL, 22);
concat_ws
------------
abcde,2,22 --->>>以第一个字符为分隔符,并忽略NULL值,拼接成一个字符串
(1 行记录)
postgres=# select concat_ws('-', 'abcde', 1, NULL, 2,null,3,null,4,null,5);
concat_ws
-----------------
abcde-1-2-3-4-5 --->>>以第一个字符为分隔符,并忽略NULL值,拼接成一个字符串
(1 行记录)
postgres=# select octet_length('类');
octet_length
--------------
3 --->>>返回Byte的个数
(1 行记录)
postgres=# select char_length('类');
char_length
-------------
1 --->>>返回字符(characters)的个数
(1 行记录)
postgres=# select bit_length('类');
bit_length
------------
24 --->>>返回bit的个数
postgres=# select overlay('Txxxas' placing 'hom' from 2 for 4);
overlay
---------
Thoms --->>>(从字符串左边开始,从1开始算)从第二位开始,数四个(本例中是xxxa),换成hom (有缩小字符串的功效)
(1 行记录)
postgres=# select overlay('Txxxxas' placing 'hom' from 2 for 4);
overlay
---------
Thomas --->>>(从字符串左边开始,从1开始算)从第二位开始,数四个(本例中是xxxx),换成hom
(1 行记录)
postgres=# select overlay('山东省济南市历下区' placing '' from 3 for 1);
overlay
------------------
山东济南市历下区 --->>>(从字符串左边开始,从1开始算)从第三位开始,数一个(本例中是省),换成‘‘
(1 行记录)
postgres=# select overlay('山东省济南市历下区' placing '---人口接近一个亿---' from 3);
overlay
--------------------------
山东---人口接近一个亿---
(1 行记录)
--->>>(从字符串左边开始,从1开始算)从第三位开始,替换成:---人口接近一个亿---
postgres=#
postgres=# select position('济南' in '山东省济南市历下区是属于济南市的一个下辖区');
position
----------
4 --->>>从1开始算,返回 in之前的字符 在 in之后的字符串中第一次出现的位置,
(1 行记录)
postgres=# select position('济南' in '山东省济南市历下区');
position
----------
4 --->>>从1开始算,返回 in之前的字符 在 in之后的字符串中第一次出现的位置,返回值是int型。
(1 行记录)
postgres=# select position('om' in 'Thomas');
position
----------
3 --->>>从1开始算,返回 in之前的字符 在 in之后的字符串中第一次出现的位置,返回值是int型。
(1 行记录)
postgres=# select substring('Thomas' from 2 for 3);
substring
-----------
hom
(1 行记录) --->>>(从字符串左边开始,从1开始算)从第二开始取,连续取出三个长度,
postgres=# select substring('Thomas' from 2);
substring
-----------
homas --->>>(从字符串左边开始,从1开始算)从第二开始取,连续取到字符串的结尾。
(1 行记录)
postgres=# select substring('山东省济南市历下区' from 2 for 3);
substring
-----------
东省济 --->>>(从字符串左边开始,从1开始算)从第二开始取,连续取出三个长度,
(1 行记录)
postgres=# select substring('山东省济南市历下区' from 2);
substring
------------------
东省济南市历下区
(1 行记录)
--->>>(从字符串左边开始,从1开始算)从第二开始取,连续取到字符串的结尾。
postgres=#

本文详细介绍了PostgreSQL数据库中的各种字符串处理函数,包括字符串连接、替换、截取、位置查找等操作,通过实例展示了如何使用这些函数进行高效的数据处理。
7375

被折叠的 条评论
为什么被折叠?



