How to delete records based on a word with underscore like MONTH_JAN'?


For this, you can use LIKE operator. Let us first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name varchar(40),
   BornMonth varchar(40)
);
Query OK, 0 rows affected (0.47 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Name,BornMonth) values('Chris','MONTH_DEC');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(Name,BornMonth) values('Bob','MONTH_JAN');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(Name,BornMonth) values('Mike','MONTH_FEB');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable(Name,BornMonth) values('David','MONTH_JAN');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(Name,BornMonth) values('Carol','MONTH_MARCH');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+-------+-------------+
| Id | Name  | BornMonth   |
+----+-------+-------------+
|  1 | Chris | MONTH_DEC   |
|  2 | Bob   | MONTH_JAN   |
|  3 | Mike  | MONTH_FEB   |
|  4 | David | MONTH_JAN   |
|  5 | Carol | MONTH_MARCH |
+----+-------+-------------+
5 rows in set (0.00 sec)

Following is the query to delete records based on a word −

mysql> delete from DemoTable where BornMonth LIKE '%MONTH_JAN%';
Query OK, 2 rows affected (0.18 sec)

Let us check the table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+----+-------+-------------+
| Id | Name  | BornMonth   |
+----+-------+-------------+
|  1 | Chris | MONTH_DEC   |
|  3 | Mike  | MONTH_FEB   |
|  5 | Carol | MONTH_MARCH |
+----+-------+-------------+
3 rows in set (0.00 sec)

Updated on: 04-Oct-2019

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements