Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query to test if string value contains carriage return

Trying to figure out the right way to test if a VARCHAR column value ends with a carriage return. Tried this but it does not work, database is Oracle 11g...

select name from myTable where name LIKE '%\r' OR name like '%\n'
like image 244
raffian Avatar asked Oct 03 '12 16:10

raffian


2 Answers

Try

SELECT name from myTable where name like '%'||chr(10) or name like '%'||chr(13)
like image 76
A.B.Cade Avatar answered Oct 27 '22 13:10

A.B.Cade


To find a value that contains non-printable characters such as carriage return or vertical tab or end of line you can use regexp_like function. In your case to display rows where a string value of a particular column contains carriage return at the end the similar query can be used.

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

Demo


Answer to the comments

Trim function, by default, deletes leading and trailing spaces and it will not delete carriage return or end of line characters. Lets carry out a simple test:

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> 
like image 32
Nick Krasnov Avatar answered Oct 27 '22 13:10

Nick Krasnov