Postgresql has a lot of built-in practical functions. Here are the combination and cutting functions
Environment: PostgreSQL 9.1.2
CENTOS 5.7 final
1, Combined function
1.concat
a. Grammar introduction
Concat function is a splicing function. null value splicing can be ignored. The splicing value has no separator. If the separator is needed, the following function concat? WS is needed.
2.concat_ws
a. Grammar introduction
2, Cut function
1.split_part
a. Grammar introduction
This function is very effective for taking the value of a specific location by separator
2.regexp_split_to_table
a. Grammar introduction
a. Grammar introduction
Environment: PostgreSQL 9.1.2
CENTOS 5.7 final
1, Combined function
1.concat
a. Grammar introduction
concat(str "any" [, str "any" [, ...]])
Concatenate all but first arguments with separators.
The first parameter is used as a separator.
NULL arguments are ignored.
b. Practical example:postgres=# create table t_kenyon(id int,name varchar(10),remark text);
CREATE TABLE
postgres=# insert into t_kenyon values(1,'test','kenyon'),(2,'just','china'),(3,'iam','lovingU');
INSERT 0 3
postgres=# insert into t_kenyon values(4,'test',null);
INSERT 0 1
postgres=# insert into t_kenyon values(5,null,'adele');
INSERT 0 1
postgres=# select * from t_kenyon;
id | name | remark
----+------+---------
1 | test | kenyon
2 | just | china
3 | iam | lovingU
4 | test |
5 | | adele
(5 rows)
postgres=# select concat(id,name,remark) from t_kenyon;
concat
-------------
1testkenyon
2justchina
3iamlovingU
4test
5adele
(5 rows)
c. descriptionConcat function is a splicing function. null value splicing can be ignored. The splicing value has no separator. If the separator is needed, the following function concat? WS is needed.
2.concat_ws
a. Grammar introduction
concat_ws(sep text, str "any" [, str "any" [,...] ])
Concatenate all but first arguments with separators.
The first parameter is used as a separator.
NULL arguments are ignored.
b. Practical applicationpostgres=# select concat_ws(',',id,name,remark) from t_kenyon;
concat_ws
---------------
1,test,kenyon
2,just,china
3,iam,lovingU
4,test
5,adele
(5 rows)
postgres=# select concat_ws('_',id,name,remark) from t_kenyon;
concat_ws
---------------
1_test_kenyon
2_just_china
3_iam_lovingU
4_test
5_adele
(5 rows)
postgres=# select concat_ws('',id,name,remark) from t_kenyon;
concat_ws
-------------
1testkenyon
2justchina
3iamlovingU
4test
5adele
(5 rows)
postgres=# select concat_ws('^_*',id,name,remark) from t_kenyon;
concat_ws
-------------------
1^_*test^_*kenyon
2^_*just^_*china
3^_*iam^_*lovingU
4^_*test
5^_*adele
(5 rows)
c. It shows that concat? WS function has more separator functions than concat function. In fact, it is the upgraded version of concat. If the separator is' ', the result is the same as concat. Concat | WS separators also support multiple characters as separators, which may be used more frequently in daily life. 2, Cut function
1.split_part
a. Grammar introduction
split_part(string text, delimiter text, field int)
Split string on delimiter and return the given field (counting from one)
b. Practical examplespostgres=# select split_part('abc~@~def~@~ghi','~@~', 2);
split_part
------------
def
(1 row)
postgres=# select split_part('now|year|month','|',3);
split_part
------------
month
(1 row)
c. descriptionThis function is very effective for taking the value of a specific location by separator
2.regexp_split_to_table
a. Grammar introduction
regexp_split_to_table(string text, pattern text [, flags text])
Split string using a POSIX regular expression as the delimiter.
b. Examples of usepostgres=# SELECT regexp_split_to_table('kenyon,love,,china,!',',');
regexp_split_to_table
-----------------------
kenyon
love
china
!
(5 rows)
--Cut by divider
postgres=# SELECT regexp_split_to_table('kenyon, china loves',E'\\s');
regexp_split_to_table
-----------------------
kenyon,
china
loves
(3 rows)
--Cut by letter
postgres=# SELECT regexp_split_to_table('kenyon,,china',E'\\s*');
regexp_split_to_table
-----------------------
k
e
n
y
o
n
,
,
c
h
i
n
a
(13 rows)
3.regexp_split_to_array a. Grammar introduction
regexp_split_to_array(string text, pattern text [, flags text ])
Split string using a POSIX regular expression as the delimiter.
b. Practical examplespostgres=# SELECT regexp_split_to_array('kenyon,love,,china,!',',');
regexp_split_to_array
--------------------------
{kenyon,love,"",china,!}
(1 row)
postgres=# SELECT regexp_split_to_array('kenyon,love,,china!','s*');
regexp_split_to_array
-----------------------------------------------
{k,e,n,y,o,n,",",l,o,v,e,",",",",c,h,i,n,a,!}
(1 row)
c. descriptionS * in the flag used above means split all, and E'\s' means escape space
From: https://my.oschina.net/Kenyon/blog/76819