postgresql 将列值拆分为 SELECT 中的两列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7524445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split a column value into two columns in a SELECT?
提问by NJ.
I have a string value in a varchar column. It is a string that has two parts. Splitting it before it hits the database is not an option.
我在 varchar 列中有一个字符串值。它是一个由两部分组成的字符串。在它到达数据库之前拆分它不是一种选择。
The column's values look like this:
该列的值如下所示:
one_column:
'part1 part2'
'part1 part2'
So what I want is a a result set that looks like:
所以我想要的是一个看起来像的结果集:
col1,col2:
part1,part2
part1,part2
How can I do this in a SELECT statement? I found a pgsql function to split the string into an array but I do not know how to get it into two columns.
如何在 SELECT 语句中执行此操作?我找到了一个 pgsql 函数来将字符串拆分成一个数组,但我不知道如何将它分成两列。
回答by Malvolio
select split_part(one_column, ' ', 1) AS part1,
split_part(one_column, ' ', 2) AS part2 ...