【问题标题】:How to make a list of quoted strings from the string values of a column in postgresql?如何从 postgresql 中列的字符串值中生成带引号的字符串列表?
【发布时间】:2022-12-08 08:11:38
【问题描述】:

从测试中选择 my_col;

出去:
my_col

X

z

如何将三行的输出更改为 postgresql 中三个带引号的字符串列表的输出,使其看起来像:

出去:

'x','y','z'

如果我运行string_agg(my_val, ''','''),我得到

出去:

x','y','z

如果我在此输出之上运行 quote_literal() ,我得到:

出去:

'x'',''y'',''z'

我需要这个带引号的字符串列表作为函数(存储过程)参数的输入。该函数通过手动传递 'x','y','z' 作为参数来工作。因此,这只是缺少前导和尾随引号。

旁注,不是针对问题的:然后它将作为variadic _v text[] 读入函数,以便我可以使用where t.v = any(_v) 在函数中检查它的值。

【问题讨论】:

    标签: sql postgresql stored-procedures


    【解决方案1】:

    你似乎想要:

    select string_agg('''' || my_val || '''', ',') my_val_agg
    from test
    

    即:连接值周围的引号聚合它们 - 然后剩下的就是在两者之间添加 , 分隔符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多