PostgreSQL database field functions and combinations of cutting

Postgresql which built many utility functions, here the combination of the cutting function and 
environment: the PostgreSQL 9.1.2 
          the CentOS  5.7 Final 

. A combined function 
1.concat 
A description.

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 examples:

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. Description concat function is purely a function of splicing, the splicing can ignore the value of a null value, no mosaic delimiter, the delimiter if necessary, you need to use the following function concat_ws. 

2.concat_ws 
A. 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. the practical application

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   
---------------
 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. concat_ws Description concat function than a function of a separator function, in fact, concat upgraded version, if the separator is '', taken out and the result is the same concat. Its function and the mysql group_concat relatively similar function, but also different, in concat_ws PG separator also supports multiple character as a separator, and used daily may be more ||.  

Second, the cutting function 
1.split_part 
A. Introduction

split_part(string text, delimiter text, field int)

Split string on delimiter and return the given field (counting from one)

b. Practical examples

postgres=# 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. described the function value by a specific location to pick up separators very effective 

2.regexp_split_to_table 
A. Introduction

regexp_split_to_table(string text, pattern text [, flags text])

Split string using a POSIX regular expression as the delimiter.

b. Use Examples

postgres=# SELECT regexp_split_to_table('kenyon,love,,china,!',',');
 regexp_split_to_table 
-----------------------
 kenyon
 love
 
 china
 !
(5 rows)

- by cutting delimiter
postgres=# SELECT regexp_split_to_table('kenyon,china,loves',',');
 regexp_split_to_table 
-----------------------
 kenyon
 china
 loves
(3 rows)

- Alphabetical cutting
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. Introduction

regexp_split_to_array(string text, pattern text [, flags text ])

Split string using a POSIX regular expression as the delimiter.

b. Practical examples

postgres=# 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. Description 
flag used in the above representation split all s *linux

Guess you like

Origin www.cnblogs.com/telwanggs/p/12105437.html