PostgreSQL中第一个空格出现时拆分字符串?
我有一个列,包含像这样的名字和姓氏PostgreSQL中第一个空格出现时拆分字符串?,sql,string,postgresql,csv,psql,Sql,String,Postgresql,Csv,Psql,我有一个列,包含像这样的名字和姓氏 word1 word2 word1 word2 word3 word1 word2 我需要在第一次出现空格时拆分字符串 |word1| |word2| |word1| |word2| |word3| |word1| |word2| 有人能帮我吗谢谢对于每个字符串的固定最大元素数,您可以使用split_part()`: 另一种方法是使用阵列: select x.arr[1] as word1, x.arr[2] as word2, x.arr[3] as w
word1 word2
word1 word2 word3
word1 word2
我需要在第一次出现空格时拆分字符串
|word1| |word2|
|word1| |word2| |word3|
|word1| |word2|
有人能帮我吗谢谢对于每个字符串的固定最大元素数,您可以使用split_part()`: 另一种方法是使用阵列:
select x.arr[1] as word1, x.arr[2] as word2, x.arr[3] as word3
from mytable t
cross join lateral (values (string_to_array(t.col, ' '))) x(arr)
这可能更有效,因为字符串只解析一次。将列拆分为一个数组:
select string_to_array(words, ' ') as words_array
如果愿意,您可以将其放在单独的列中,但我会将字符串\u to_array()
移动到from
子句:
select word_array[1], word_array[2], word_array[3]
from t cross join lateral
(values (string_to_array(t.words, ' '))) v(word_array)
这取决于句子中有多少单词,我认为第一个单词很简单
SELECT split_part(colName,' ',1);
对于第一个空格之后的其他所有内容,它变得有点复杂。
您可以使用数学或正则表达式。我认为像好的古老数学一样的东西可以奏效
SELECT substring(colname,(length(split_part(colname,' ',1)))+2,(length(colname)) - (length(split_part(colname,' ',1))+1));
或者您可以使用函数
select regexp\u replace
和正则表达式,除了添加的管道,您的输出与输入有何不同?
SELECT substring(colname,(length(split_part(colname,' ',1)))+2,(length(colname)) - (length(split_part(colname,' ',1))+1));