【问题标题】:Need the output in specific format in psql需要在 psql 中以特定格式输出
【发布时间】:2018-08-27 11:50:53
【问题描述】:

假设:-

subh=# select 'test123';
          ?column?
-------------------------------------
         test123

subh=# select obj_description('test123'::regclass);
  obj_description
--------------------
 this is my table

我正在运行这个查询:-

subh=# select 'test123' || ' ' || obj_description('test123'::regclass) as test;
             test
 -------------------------------
  test123 this is my table

其实我希望输出如下:-

            test
-------------------------------
  test123 'this is my table'

【问题讨论】:

  • ''' ' || obj_description('test123'::regclass) || ''''

标签: sql concat greenplum


【解决方案1】:

使用quote_literal

此函数在给定字符串/数字的前后添加单引号。阅读here

subh=# select 'test123' || ' ' || quote_literal(obj_description('test123'::regclass)) as test;

【讨论】:

    【解决方案2】:
    subh=# select 'test123' || ' \'' || obj_description('test123'::regclass) as test || '\'';
    
                 test
     -------------------------------
      test123 'this is my table'
    

    \' 将用于添加报价

    【讨论】:

    • 引号在 SQL 中使用反斜杠转义,而是通过将它们加倍:'test123' || '''' || ....
    猜你喜欢
    • 1970-01-01
    • 2021-01-04
    • 2015-05-28
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多