我正在尝试转换PostgresSQL中的一列:
以下是col_A中的一些值:
col_A
---------
John_arrived
Mary_Brown_arrived
J_C_Jr_arrived
Q_arrived
目标是仅获取col_A的第一部分和最后部分:
d_col_A
-------
John_arrived
Mary_arrived
J_arrived
Q_arrived
这是我的问题:
with t1 as (
select split_part(col_A, '_'::text, 1) || '_'
|| COALESCE (split_part(col_A, '_'::text, -1), '' ) as d_col_A
from my_table
)
select distinct d_col_A from t1
然后我收到了以下错误:
Invalid Length
Detail:
-----------------------------------------------
error: Invalid Length
code: 8001
context: field position must be greater than zero
query: 2382655
location: funcs_string.cpp:1565
process: query0_27 [pid=11502]
-----------------------------------------------
知道我做错了什么吗?谢谢!
答案 0 :(得分:0)
如果您想要列的最后一部分,则不能使用-1。您需要反转列并获得第一部分并再次反转:
reverse(split_part(reverse(col_A), '_'::text, 1))