Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape line breaks in MySQL output

Tags:

mysql

I have a a column I want to inspect that contains strings with newlines in them. I want to be able to explicitly see the \n and \r characters that are in these strings. Is there some way to write my SQL query to tell it to output the strings with the newlines escaped?

like image 828
Kevin Avatar asked Jun 26 '13 16:06

Kevin


People also ask

What is line break in MySQL?

To specify a line break in MySQL, we need the function CHAR(). CHAR() takes an ASCII code and returns the corresponding character. The code for a line break is 10, for a carriage return it is 13. So we can express the following MySQL query for example: UPDATE tab SET col = REPLACE (col, CHAR (13, 10), '' );

How do I remove extra spaces between words in MySQL?

If you have spaces between letters then you can use REPLACE() function to remove spaces.

How do I escape backslash in MySQL?

Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”.


1 Answers

SELECT REPLACE(
    REPLACE(yourcolumn, '\r', '\\r'),
    '\n',
    '\\n'
) FROM yourtable;
like image 169
RandomSeed Avatar answered Oct 14 '22 20:10

RandomSeed