用于测试字符串值是否包含回车符的SQL查询

raf*_*ian 8 sql oracle comparison sql-like

试图找出测试VARCHAR列值是否以回车符结束的正确方法.试过这个,但它不起作用,数据库是Oracle 11g......

select name from myTable where name LIKE '%\r' OR name like '%\n'
Run Code Online (Sandbox Code Playgroud)

A.B*_*ade 11

尝试

SELECT name from myTable where name like '%'||chr(10) or name like '%'||chr(13)
Run Code Online (Sandbox Code Playgroud)


Nic*_*nov 5

要查找包含不可打印字符的值(如回车符或垂直制表符或行尾),可以使用regexp_like函数.在您的情况下,为了显示特定列的字符串值在末尾包含回车符的行,可以使用类似的查询.

select *
  from your_table_name
 where regexp_like(trim(string_column), '[[:space:]]$')
Run Code Online (Sandbox Code Playgroud)

演示


回答评论

Trim默认情况下,函数删除前导和尾随空格,它不会删除回车符行尾字符.让我们进行一个简单的测试:

SQL> create table Test_Table(
  2    id number,
  3    col1 varchar2(101)
  4  );

Table created

SQL> insert into Test_Table (id, col1)
  2    values(1, 'Simple string');

1 row inserted

SQL> commit;

Commit complete

SQL> insert into Test_Table (id, col1)
  2    values(1, 'Simple string with carriage return at the end' || chr(13));

1 row inserted

SQL> commit;

Commit complete

SQL> insert into Test_Table (id, col1)
  2    values(1, '   Simple string with carriage return at the end leading and trailing spaces' || chr(13)||'   ');

1 row inserted

SQL> commit;

Commit complete

SQL> insert into Test_Table (id, col1)
  2    values(1, '  Simple string leading and trailing spaces  ');

1 row inserted

SQL> commit;

Commit complete

SQL> select *
  2    from test_table;

        ID COL1
--------------------------------------------------------------------------------
         1 Simple string
         1 Simple string with carriage return at the end
         1    Simple string with carriage return at the end leading and trailing spaces
         1   Simple string leading and trailing spaces

SQL> 
SQL> select *
  2    from test_table
  3   where regexp_like(trim(col1), '[[:space:]]$')
  4  ;

        ID COL1
----------------------------------------------------------------------------------
         1 Simple string with carriage return at the end
         1    Simple string with carriage return at the end leading and trailing spaces

SQL> 
Run Code Online (Sandbox Code Playgroud)