Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL search if a string contains special characters?

I need to search table field contains special characters. I found a solution given here something like:

SELECT * 
FROM `tableName` 
WHERE `columnName` LIKE "%#%" 
OR `columnName` LIKE "%$%" 
OR (etc.)

But this solution is too broad. I need to mention all the special characters. But I want something which search something like:

SELECT *
FROM `tableName`
WHERE `columnName` LIKE '%[^a-zA-Z0-9]%'

That is search column which contains not only a-z, A-Z and 0-9 but some other characters also. Is it possible with MYSQL

like image 633
Al Amin Chayan Avatar asked Nov 05 '15 07:11

Al Amin Chayan


People also ask

Which operator in MySQL is used to check string contains specific characters?

MySQL LIKE operator checks whether a specific character string matches a specified pattern. Pattern matching using SQL simple regular expression comparison. Returns 1 (TRUE) or 0 (FALSE). If either expr or pat is NULL, the result is NULL.


1 Answers

Use regexp

SELECT *
FROM `tableName`
WHERE `columnName` REGEXP '[^a-zA-Z0-9]'

This would select all the rows where the particular column contain atleast one non-alphanumeric character.

or

REGEXP '[^[:alnum:]]'
like image 53
Avinash Raj Avatar answered Sep 18 '22 18:09

Avinash Raj