I have to remove the newlines character if the column has any, but with ‘%/r/n%,’ I can’t locate any record, and here’s how to solve the problem.

SQL

I am using PostgreSQL and rails in this case. In SQL record, I can see the result, including the content like ‘example /r/n example,’ but whenever I use the query

select *
from table
where column like '%/r/n%'

I will have no result.

turns out

I do not need to put escape here, I need to un-escape it, according to this amazing stackflow post and basically, I just need to change the query to

select *
from table
where column like E'%\n%'

or literally(which is so cool), put a newlines in the query like

select *
from table
where column like '%
%'

And you will get the result records!

ActiveRecord

For ruby on rails with ActiveRecord interface i use:

Table.where('column like ?','%
%')

And it will return the proper record.

I hope this helps you as it helps me a lot!

Happy coding!

Comments