如何选择用引号引起来的字段?

问题描述 投票:0回答:4

我尝试选择一些字段并将它们括在引号内,因为我必须将这些记录导出到平面文件中,然后将它们插入到另一个数据库中。因此,我在 Oracle 上运行这些查询,但在尝试添加这些引号时遇到问题。这是我尝试运行的查询:

SELECT
  'connection_string' || '|' || 
  ''' || employee_name || '''
FROM
  table1

我想要的输出是:

'connection_string'|'Bob'
'connection_string'|'Jane'

我也尝试过

SELECT
  'connection_string' || '|' || 
  '' || employee_name || ''
FROM
  table1

这显然行不通。连接字符串是静态的并保持不变,结果应该由管道分隔

sql oracle quote
4个回答
2
投票

您只是在寻找输出中的单引号吗?如果是这样:

SELECT '''' || 'connection_string' || '''|''' || 
       employee_name || ''''
FROM table1;

字符串中连续的两个单引号代表一个单引号。或者,换句话说,对于带有单个单引号的字符串,您需要连续四个:

''''
^ starts the string
-^^ the single quote
---^ ends the string

0
投票

你的意思是这样吗?使用 SCOTT 模式中的 EMP 表。

select ename, '''connection_string''|''' || ename || ''''
from   scott.emp;

ENAME      '''CONNECTION_STRING''|'''||ENAM
---------- --------------------------------
SMITH      'connection_string'|'SMITH'
ALLEN      'connection_string'|'ALLEN'
WARD       'connection_string'|'WARD'
JONES      'connection_string'|'JONES'
MARTIN     'connection_string'|'MARTIN'
BLAKE      'connection_string'|'BLAKE'
CLARK      'connection_string'|'CLARK'
SCOTT      'connection_string'|'SCOTT'
KING       'connection_string'|'KING'
TURNER     'connection_string'|'TURNER'
ADAMS      'connection_string'|'ADAMS'
JAMES      'connection_string'|'JAMES'
FORD       'connection_string'|'FORD'
MILLER     'connection_string'|'MILLER'

0
投票

另一种方法可以使用 Q 语法:

SQL> with tableA(a) as (
  2    select 'aaa' from dual union all
  3    select 'AAA' from dual
  4  )
  5  select q'[Your string ']' || a || q'[']' as A
  6  from tableA;

A
-----------------
Your string 'aaa'
Your string 'AAA'

在这种情况下它可能有点多余,但在更复杂的情况下它可能很有用。


0
投票
select listagg( '''' || TESTNAME || '''',',') from test;

这将导致数据 单引号将所有测试名称用逗号分隔并用单引号括起来

© www.soinside.com 2019 - 2024. All rights reserved.