Postgresql里面内置了很多的实用函数,下面介绍下组合和切割函数
一.组合函数
1.concat
a.语法介绍

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.实际例子:

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.说明
concat函数纯粹是一个拼接函数,可以忽略null值拼接,拼接的值没有分隔符,如果需要分割符,则需要用下面的函数concat_ws。

2.concat_ws
a.语法介绍

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.实际应用

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函数比concat函数多了分隔符的功能,其实就是concat的升级版,假如分隔符为'',则取出来的结果和concat是一样的。concat_ws分隔符还支持多个字符作为分隔符的,日常用得更多的可能是||。 

二、切割函数
1.split_part
a.语法介绍

split_part(string text, delimiter text, field int)

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

b.实际例子

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.说明
该函数对按分隔符去取某个特定位置上的值非常有效果

2.regexp_split_to_table
a.语法介绍

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

Split string using a POSIX regular expression as the delimiter.

b.使用例子

postgres=# SELECT regexp_split_to_table('kenyon,love,,china,!',',');
regexp_split_to_table
-----------------------
kenyon
love china
!
(5 rows) --按分割符切割
postgres=# SELECT regexp_split_to_table('kenyon, china loves',E'\\s');
regexp_split_to_table
-----------------------
kenyon,
china
loves
(3 rows) --按字母切割
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.语法介绍

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

Split string using a POSIX regular expression as the delimiter.

b.实际例子

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.说明
上面用到的flag里的s*表示split all,E'\\s'表示转义空格

PostgreSQL数据库切割和组合字段函数的更多相关文章

  1. Python numpy插入、读取至postgreSQL数据库中bytea类型字段

    安装psycopg2模块,此模块用于连接PostgreSQL数据库 ​pip install psycopg2 # -*- coding: utf-8 -*- import psycopg2 impo ...

  2. Postgresql数据库的一些字符串操作函数

    今天做项目遇到客户反映了一个麻烦的事情,有一些数据存在,但就是在程序中搜索不出来,后来分析,发现问题为数据前面有几个空白字符,后来用SQL查询了一下,发现八九个数据表中,数千万条数据中有将近三百万条数 ...

  3. SqlServer判断数据库、表、字段、存储过程、函数是否存在

    原文:SqlServer判断数据库.表.字段.存储过程.函数是否存在 判断数据库是否存在 if exists (select * from sys.databases where name = '数据 ...

  4. PostgreSQL介绍以及如何开发框架中使用PostgreSQL数据库

    最近准备下PostgreSQL数据库开发的相关知识,本文把总结的PPT内容通过博客记录分享,本随笔的主要内容是介绍PostgreSQL数据库的基础信息,以及如何在我们的开发框架中使用PostgreSQ ...

  5. vs2010连接postgresql数据库

    Windows环境C/C++访问PostgreSQL主要有两种方式:利用Qt封装的数据库访问组件.利用PostgreSQL的API函数.使用Qt平台访问PostgreSQL的局限性很大,一旦脱离了访问 ...

  6. pg_restore - 从一个由 pg_dump 创建的备份文件中恢复 PostgreSQL 数据库。

    SYNOPSIS pg_restore [ option...] [ filename] DESCRIPTION 描述 pg_restore 是一种用于恢复由 pg_dump(1) 创建的任何非纯文本 ...

  7. Oracle数据库迁移至PostgreSQL数据库问题及解决

    Oracle数据库迁移PostgreSQL数据库问题及解决 目录 如何计划迁移数据库(现状及问题分析) 统计系统表及表功能 解耦公共表 建立数据库 迁移表结构 导入表数据 改SQL语法 保证数据时效性 ...

  8. [转载]php连接postgreSQL数据库及其操作(php5,postgreSQL9)

    数据库连接:dbconn.php<?php$conn = pg_connect("host=localhost port=5432 dbname=myd user=postgres p ...

  9. Spring Boot中使用PostgreSQL数据库

    在如今的关系型数据库中,有两个开源产品是你必须知道的.其中一个是MySQL,相信关注我的小伙伴们一定都不陌生,因为之前的Spring Boot关于关系型数据库的所有例子都是对MySQL来介绍的.而今天 ...

  10. MySQL&SQL server&Oracle&Access&PostgreSQL数据库sql注入详解

    判断数据库的类型 当我们通过一些测试,发现存在SQL注入之后,首先要做的就是判断数据库的类型. 常用的数据库有MySQL.Access.SQLServer.Oracle.PostgreSQL.虽然绝大 ...

随机推荐

  1. ast在爬虫上的应用

    https://astexplorer.net/ https://zhuanlan.zhihu.com/p/371710865 1.基础了解 const {parse} =require(" ...

  2. c++随笔测试(Corner of cpp)

    在c++17下,程序的输出是什么?(有可能编译出错,有可能输出未知,有可能是未定义行为) 点击查看代码 #include<iostream> void foo(unsigned int) ...

  3. [深度学习]DEEP LEARNING(深度学习)学习笔记整理

    转载于博客http://blog.csdn.net/zouxy09 一.概述 Artificial Intelligence,也就是人工智能,就像长生不老和星际漫游一样,是人类最美好的梦想之中的一个. ...

  4. .NET周报【1月第1期 2023-01-06】

    国内文章 [开源]基于.net6+gtksharp实现的Linux下的图形界面串口调试工具 https://www.cnblogs.com/flykai/p/17007554.html 由于公司的上位 ...

  5. 学习记录C

    学了这么久,终于开始实训项目了....... 奥里给 !!! 压力好大,好喜欢什么也不想的时候 记录学习的代码 分享一下 /* system函数:( #include<stdlib.h> ...

  6. Mac上安装brew的那些坑

    macOS11.1 入坑! 网上看了一下午的帖子,包括官网,重装command line tool,修改brew_install文件,报错443,Faild during:git getch错误 脱坑 ...

  7. Flutter 耗时监控 | 路由名为空原因分析

    前言 最近群里遇到获取Route名为空的问题,当时没在意... 直到自己在监控页面启动耗时,需要确定当前页面是哪个从而方便标记它加载的耗时时,遇到同样 route.settings.name 为空问题 ...

  8. BOM操作、DOM操作、jQuery类库

    BOM操作.DOM操作.jQuery类库 一.BOM操作 BOM(Browser Object Model)是指浏览器对象模型,它使JavaScript有能力与浏览器进行对话 1.window对象 浏 ...

  9. 阅读B2B使用手册随感

    EDI 协议中最重要的协议之一AS2协议,几乎成为对安全性要求比较高的企业.监管机构的首选.在零售.物流.医药等行业,EDI都有广泛应用.据我所知,在医药行业,几乎全球重要的监管机构都在使用EDI, ...

  10. ubuntu下yaml-cpp安装与使用

    安装 从GitHub上下载源码编译安装:git clone https://github.com/jbeder/yaml-cpp.git: 进入源码目录并创建一个 build 目录:cd yaml-c ...