Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a column with Non-accented characters using select query

I have a MySQL table (test) with utf-8 charset encoding. There are three entries, two entries with normal characters and another name with accent characters.

CREATE TABLE test (
  id Integer,
  name VARCHAR(50), 
  PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

INSERT INTO `test` (`id`, `name`) VALUES (1, 'aaaa');
INSERT INTO `test` (`id`, `name`) VALUES (2, 'AAAA');
INSERT INTO `test` (`id`, `name`) VALUES (3, 'áááá');

If I run the following select query, it returns all the 3 entries

Actual Result:-

select * from test where name like '%aa%';

id  | name
----|----
1   | aaaa
2   | AAAA
3   | áááá

Instead of that, it should be return last entry with id=3.

I don't want to use 'BINARY' OR 'COLLATE utf8_bin' because it returns only case sensitive search.

I need normal search with string like query, e.g:-

Expected Result:-

select * from test where name like '%aa%';

id | name
---|-----
1  | aaaa
2  | AAAA
like image 733
SST Avatar asked May 02 '16 10:05

SST


People also ask

How do you handle special characters in SQL query?

Use braces to escape a string of characters or symbols. Everything within a set of braces in considered part of the escape sequence. When you use braces to escape a single character, the escaped character becomes a separate token in the query. Use the backslash character to escape a single character or symbol.

How do I remove non English characters in SQL?

Answers. Hi smirnov, WHERE [Text1] NOT LIKE '%[a-z,A-Z,0-9]%' works for me.

How do I exclude a SQL filter?

In the Object Explorer, right-click the database, select Other SQL Source Control Tasks, and click Edit Filter Rules. The Edit Filter Rules dialog box is displayed. In the box, make sure Exclude if is selected.


1 Answers

The utf8_bin collation is what you need for your requirement to handle accents

I don't want to use 'BINARY' OR 'COLLATE utf8_bin' because it returns only case sensitive search.

This is easier (and more performant) to solve with utf8_bin than solving the accent issue with another collation

SELECT * FROM test WHERE LOWER(name) like '%aa%' COLLATE utf8_bin

-> added after comments

The query above assumes that the query parameters are minuscule but if you cant modify the params to always be minuscules then you can also use this variation

SELECT * FROM test WHERE LOWER(name) like LOWER('%ÚÙ%') COLLATE utf8_bin
like image 58
user3802077 Avatar answered Oct 13 '22 00:10

user3802077