How to search for “ñ” and avoid records that include “n” in MySQL?

MySQLMySQLi Database

If you do not want all records that include “n” when you search for “ñ”, use the following syntax −

select *from yourTableName where yourColumnName LIKE '%ñ%' COLLATE utf8_spanish_ci;

Let us first create a table. Following is the query −

mysql> create table NotIncludenDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ClientName varchar(20)
   -> );
Query OK, 0 rows affected (1.07 sec)

Following is the query to insert some records in the table using insert command −

mysql> insert into NotIncludenDemo(ClientName) values('John');
Query OK, 1 row affected (0.21 sec)

mysql> insert into NotIncludenDemo(ClientName) values('Johñ');
Query OK, 1 row affected (0.14 sec)

mysql> insert into NotIncludenDemo(ClientName) values('Johnñy');
Query OK, 1 row affected (0.18 sec)

Following is the query to display all records from the table using select statement −

mysql> select *from NotIncludenDemo;

This will produce the following output −

+----+------------+
| Id | ClientName |
+----+------------+
| 1 | John |
| 2 | Johñ |
| 3 | Johnñy |
+----+------------+
3 rows in set (0.00 sec)

Following is the query to search for “ñ” −

mysql> select *from NotIncludenDemo where ClientName LIKE '%ñ%' COLLATE
utf8_spanish_ci;

This will produce the following output −

+----+------------+
| Id | ClientName |
+----+------------+
| 2 | Johñ |
| 3 | Johnñy |
+----+------------+
2 rows in set (0.60 sec)
raja
Published on 15-Apr-2019 15:19:25
Advertisements