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

时间:2012-10-03 16:09:07

标签: sql oracle comparison sql-like

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

select name from myTable where name LIKE '%\r' OR name like '%\n'

2 个答案:

答案 0 :(得分:11)

尝试

SELECT name from myTable where name like '%'||chr(10) or name like '%'||chr(13)

答案 1 :(得分:5)

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

select *
  from your_table_name
 where regexp_like(trim(string_column), '[[:space:]]$')

Demo


回答评论

默认情况下,

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>