【问题标题】:Invalid Length: field position must be greater than zero长度无效:字段位置必须大于零
【发布时间】:2018-03-30 21:03:01
【问题描述】:

我正在尝试转换 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]
  -----------------------------------------------

知道我做错了什么吗?谢谢!

【问题讨论】:

    标签: sql postgresql concatenation


    【解决方案1】:

    如果您想要列的最后一部分,则不能使用 -1。您需要反转列并获取第一部分并再次反转:

    reverse(split_part(reverse(col_A), '_'::text, 1))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 2022-08-18
      • 1970-01-01
      相关资源
      最近更新 更多