Returning records where column has carriage return line feed

How do I write an SQL statement that will return all the records for a table where Column A contains a carriage return line feed?

That's a very good question; there are two methods I can think of.

Code the CR and LF characters directly into the LIKE expression. This relies on the fact that the CR and LF characters are ASCII standard characters, and so (on Windows keyboards anyway) you can enter them by holding down the Alt key while entering the appropriate 4-digit number in the numeric keypad area on the right side of the keyboard (i.e. not using the digits above the qwerty row). The carriage return character is Alt-0013 and the line feed character is Alt-0010. Make sure the Num Lock key is on.

You will likely not be able to see these characters, since whatever tool you're entering them into (e.g. Notepad, Query Analyzer, SQL window) will probably interpret them as a -- wait for it -- carriage return and line feed!

select * from yourTable
 where ColumnA like '%

You may have trouble with cut & paste and getting this to run, but I got it to work on an Oracle system.

Write the expression using a database function. This of course is the better way to go (although not all databases may let you code a concatenated LIKE expression). In MySQL, for example, you would use the HEX function

select * from yourTable
 where ColumnA like '%'+hex(13)+hex(10)+'%'

(except that I don't have access to MySQL and so don't know if this will actually run).

In SQL/Server, you would use the CHAR() function or the 0xNN notation --

select * from yourTable
 where ColumnA like '%'+0x0D+0x0A+'%'

where 0D and 0A are the hexadecimal representations of the CR and LF.

For More Information

  • What do you think about this answer? E-mail the edtiors at [email protected] with your feedback.
  • The Best SQL Web Links: tips, tutorials, scripts, and more.
  • Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

Dig Deeper on Oracle development languages

Data Management
Business Analytics
Data Center
Content Management