Categories
SQL / MySQL Tips and Tutorials

What are New Line Feed and Carriage Return in a MySQL query?

We all know what a new line is represented in c and in PHP as:

"\n"

And a carriage return:

"\r"

In a SQL / MySQL query, when you want to add new lines or line feeds into the strings, you will need the CHAR() function of MySQL which accepts an ASCII character code as input and returns a character accordingly. For example, to search a specific column for all new lines and erase them:

UPDATE sometable SET somecolumn = REPLACE(somecolumn, CHAR(10), ''); // CHAR(10) is a new line

This could get the thing done in most cases. However in some systems new lines are a combination of a new line and a carriage return, namely "\r\n". In this case, you will need CHAR(13):

UPDATE sometable SET somecolumn = REPLACE(somecolumn, CHAR(13) + CHAR(10), ''); // CHAR(13) is a carriage return and the plus sign is how strings are concatenated in MySQL.

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.

1 reply on “What are New Line Feed and Carriage Return in a MySQL query?”

Comments are closed.