Problem

Today, I wanted to strip slashes from a comment field using SQL (INSTEAD of using php stripslashes).

Why you ask?

I was working on an application which had not considered stripslashes for formatting output.

So I had a look and realised. Do I change 40,000 lines of code to add Stripslashes, or 1 line at the SQL select statement?

OK, slight exaggeration, but you get what I mean.

Solution

MySQL

[sourcecode language=’sql’]
Select
Replace(field_name, ‘\’, ”) as stripped_field
from
table;
[/sourcecode]

Oracle

[sourcecode language=’sql’]
Select
Replace(field_name, ”, ”) as stripped_field
from
table;
[/sourcecode]

PHP EXTRA

If using in PHP add an extra backslash like this;

[sourcecode language=’sql’]
// strip slashes as part of the SQL select statement
$sql = “Select Replace(field_name, ‘\’, ”) as stripped_field from table”;
[/sourcecode]

Please get in touch if you know of an easier way to do this, for example, Oracle Functions, or MySQL functions.

Many thanks and happy coding.