如何识别变量是否在Oracle中包含换行符

时间:2015-04-21 06:32:52

标签: sql oracle oracle11g newline

我有一个过程,它接受2个变量作为输入,其中一个可能有由enter或space分隔的字符串字符。

然而,空间很容易理解,但我们如何判断变量中是否有换行符。

我尝试使用chr(10)但没有用,因为我必须识别字符串是否由换行符分隔。顺便说一句,即使chr(10)无法插入用空格划分的字符串。

select 'ABC' || chr(10) || 'DEF' as c from dual

我目前没有这样的变量作为输入,所以甚至无法尝试缩小到某些解决方案,而且上面的查询运行不正常,我的意思是它没有给出如下的输出。

ABC
DEF

我还搜索了不同的oracle文档,但没有找到任何文档。

帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

create table sample (a varchar2(100));
insert into sample (a) values ('HI_next_line
hello_next_line
hello2');
insert into sample (a) values ('singleline1');
insert into sample (a) values ('singleline2');

SQL> select * from sample;

A
-----------------------------------------------------------------------
HI_next_line
hello_next_line
hello2

singleline1
singleline2

SQL> select *
  2  from sample
  3  where instr(a, chr(10)) > 0;

A
-----------------------------------------------------------------------
HI_next_line
hello_next_line
hello2

答案 1 :(得分:1)

新行取决于操作系统。在基于 Unix 的操作系统中,它是CHR(10),在 Windows 中,它是CHR(13) ,然后是 CHR(10)

您可以使用以下任一方法:

  • LIKE '%'||chr(10)||'%'
  • INSTR(column_name, chr(10)) > 0

让我们看看Windows操作系统中的测试用例:

SQL

使用LIKE

SQL> WITH DATA AS(
  2  SELECT 'ABC' || chr(10) || 'DEF' AS c FROM dual UNION ALL
  3  SELECT 'PQR' || ' ' || 'XYZ' AS c FROM dual UNION ALL
  4  SELECT 'QWE' || CHR(13) || 'RTY' AS c FROM dual UNION ALL
  5  SELECT 'no_space' AS c FROM dual
  6  )
  7  SELECT * FROM DATA WHERE c LIKE '%'||chr(10)||'%';

C
--------
ABC
DEF


SQL>

使用INSTR

SQL> WITH DATA AS(
  2  SELECT 'ABC' || chr(10) || 'DEF' AS c FROM dual UNION ALL
  3  SELECT 'PQR' || ' ' || 'XYZ' AS c FROM dual UNION ALL
  4  SELECT 'QWE' || CHR(13) || 'RTY' AS c FROM dual UNION ALL
  5  SELECT 'no_space' AS c FROM dual
  6  )
  7  SELECT * FROM DATA WHERE INSTR(c, chr(10)) > 0;

C
--------
ABC
DEF


SQL>

PL / SQL

使用LIKE

SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2    c VARCHAR2(100);
  3  BEGIN
  4    c:='ABC' || chr(10) || 'DEF';
  5    IF c LIKE '%'||chr(10)||'%' THEN
  6      dbms_output.put_line('found chr(10)');
  7    ELSE
  8      dbms_output.put_line('not found');
  9    END IF;
 10  END;
 11  /
found chr(10)

PL/SQL procedure successfully completed.

SQL>

使用INSTR

SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2    c VARCHAR2(100);
  3  BEGIN
  4    c:='ABC' || chr(10) || 'DEF';
  5    IF INSTR(c, chr(10)) > 0 THEN
  6      dbms_output.put_line('found chr(10)');
  7    ELSE
  8      dbms_output.put_line('not found');
  9    END IF;
 10  END;
 11  /
found chr(10)

PL/SQL procedure successfully completed.

SQL>