PostgreSQL 9.3:将一列拆分为多个 [英] PostgreSQL 9.3: Split one column into multiple

查看:1169
本文介绍了PostgreSQL 9.3:将一列拆分为多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,我想将 colb 的一列拆分为两列
,例如 column1 column2

I want to split one column that is colb in the given below example into two columns like column1 and column2.

我有一个包含两列的表:

I have a table with two columns:

示例

create table t3
(
    cola varchar,
    colb varchar
);

插入:

insert into t3 values('D1','2021to123'),
                     ('D2','112to24201'),
                     ('D3','51to201');

我想将 colb 值拆分为两列,例如以下预期结果:

I want to split the colb values into two columns like the following expected result:

预期结果

cola      column1        column2
---------------------------------
D1        2021           123
D2        112            24201
D3        51             201


推荐答案

select cola
      ,split_part(colb, 'to', 1) col1
      ,split_part(colb, 'to', 2) col2 
from t3

从PostgreSQL文档引用:

Quoted from the PostgreSQL Documentation:


split_part(字符串文本,定界符文本,字段int)

split_part(string text, delimiter text, field int)

在定界符上分割字符串并返回给定的字段(计数从
一个)

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

这篇关于PostgreSQL 9.3:将一列拆分为多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆