PostgreSQL 9.3:将一列拆分为多个

编程入门 行业动态 更新时间:2024-10-28 14:33:32
本文介绍了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:将一列拆分为多个

本文发布于:2023-10-18 23:36:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   PostgreSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!