Mysql select without special characters

I am wondering if it's possible to search through a MySQL field where the field may have something like this:

test - hello

but you have in a string from a user

test: hello

Obviously it's easy on PHP-side to strip the string of any special characters like that, but is it possible to search through MySQL rows and ignore special characters?

Mysql select without special characters

Racil Hilan

24k12 gold badges48 silver badges52 bronze badges

asked Oct 14, 2016 at 18:31

NaughtySquidNaughtySquid

1,8672 gold badges28 silver badges43 bronze badges

1

Another unique solution is to put wildcards in between each word. As long as the search phrase does not have special characters in it, the correct results will be returned while ignoring any special characters in the results.

For example...

SELECT *
FROM myTable
WHERE somefield LIKE '%test%hello%'

answered Feb 24, 2019 at 1:30

It could be possible if you find and replace all such special character and spaces from user input also column, i.e.

select * from tablename where replace(replace(columnname,' ',''),':',''),'-','')=replace(replace([USER INPUT],' ',''),':',''),'-','');

answered Oct 14, 2016 at 19:58

Mysql select without special characters

Faizan YounusFaizan Younus

7661 gold badge8 silver badges13 bronze badges

1

You can sort of "ignore" special characters, whitespace, etc. by using the SOUNDEX() function:

mysql> select soundex('test - hello'), soundex('test: hello');
+-------------------------+------------------------+
| soundex('test - hello') | soundex('test: hello') |
+-------------------------+------------------------+
| T234                    | T234                   |
+-------------------------+------------------------+

So you can search your data like this:

SELECT ...
FROM MyTable
WHERE SOUNDEX(somefield) = SOUNDEX('test: hello');

This won't be indexable at all, so it'll be forced to do a table-scan. If you use MySQL 5.7, you could add a virtual column for the soundex expression, and index that virtual column. That would help performance a lot.

answered Oct 17, 2016 at 16:55

Mysql select without special characters

Bill KarwinBill Karwin

509k83 gold badges642 silver badges803 bronze badges

2

Not the answer you're looking for? Browse other questions tagged php mysql or ask your own question.

Posted on Dec 19, 2021

Learn how to escape special characters in a MySQL SELECT statement

When you’re writing a MySQL query, there may be times when you need to include special characters in your statement.

For example, suppose you want to include a quote symbol ' inside your SELECT statement like this:

SELECT 'Hello, I'm Nathan';

The above query will trigger ERROR 1064 because you are putting a quote symbol ' that’s used as a delimiter to the string.

To fix the error, you need to escape the character from being interpreted as the delimiter of a string.

In MySQL, you can escape quote symbols by alternating between the single and double quote symbols.

If you’re using a single quote symbol in your string, use the double quote for the string delimiter as shown below:

SELECT "Hello, I'm Nathan";

-- Output:
-- +-------------------+
-- | Hello, I'm Nathan |
-- +-------------------+
-- | Hello, I'm Nathan |
-- +-------------------+

You can also do it like this:

SELECT 'Hello, I"m Nathan';

-- Output:
-- +-------------------+
-- | Hello, I"m Nathan |
-- +-------------------+
-- | Hello, I"m Nathan |
-- +-------------------+

Alternatively, MySQL also has special character escape sequences as shown below:

  • \0 - An ASCII NUL (0x00) character.
  • \' - A single quote (') character.
  • \" - A double quote (") character.
  • \b - A backspace character.
  • \n - A newline (linefeed) character.
  • \r - A carriage return character.
  • \t - A tab character.
  • \Z - ASCII 26 (Control-Z).
  • \\ - A backslash (\) character.
  • \% - A % character.
  • \_ - A _ character.

By using the above syntax, you can add the characters mentioned without being interpreted by MySQL.

Take a look at the following example:

SELECT 'Hello, I\'m Nathan';

-- Output:
-- +-------------------+
-- | Hello, I'm Nathan |
-- +-------------------+
-- | Hello, I'm Nathan |
-- +-------------------+

To add a newline character, use the \n escape sequence as shown below:

SELECT 'Hello, my name \n is Nathan';

-- Output:
-- +----------------------------+
-- | Hello, my name 
--  is Nathan |
-- +----------------------------+
-- | Hello, my name 
--  is Nathan |
-- +----------------------------+

And that’s how you include special characters in a MySQL SELECT query.

How do I remove special characters from a MySQL query?

You can remove special characters from a database field using REPLACE() function. The special characters are double quotes (“ “), Number sign (#), dollar sign($), percent (%) etc.

How do I remove a specific character from a string in MySQL?

Remove characters from string using TRIM() This section will remove the characters from the string using the TRIM() function of MySQL. TRIM() function is used to remove any character/ whitespace from the start/ end or both from a string. Let us move ahead by looking into its syntax and application. Name of the table.

How do I find special characters in MySQL?

Alternatively, MySQL also has special character escape sequences as shown below:.
\0 - An ASCII NUL (0x00) character..
\' - A single quote ( ' ) character..
\" - A double quote ( " ) character..
\b - A backspace character..
\n - A newline (linefeed) character..
\r - A carriage return character..
\t - A tab character..