How to remove only the first word from columns values with a MySQL query?

MySQLMySQLi Database

To remove only the first word from column values, use substring(). Following is the syntax −

select substring(yourColumnName,locate(' ',yourColumnName)+1) AS anyAliasName from yourTableName;

Let us first create a table −

mysql> create table DemoTable
(
   Title text
);
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Java in Depth');
Query OK, 1 row affected (0.49 sec)
mysql> insert into DemoTable values('C++ is an object oriented programming language');
Query OK, 1 row affected (0.47 sec)
mysql> insert into DemoTable values('MySQL is a relational database');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Python with data structure');
Query OK, 1 row affected (0.24 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------------------------------------------+
| Title                                          |
+------------------------------------------------+
| Java in Depth                                  |
| C++ is an object oriented programming language |
| MySQL is a relational database                 |
| Python with data structure                     |
+------------------------------------------------+
4 rows in set (0.00 sec)

Following is the query to remove the first word from column values −

mysql> select substring(Title,locate(' ',Title)+1) AS RemoveFirstWord from DemoTable;

This will produce the following output −

+--------------------------------------------+
| RemoveFirstWord                            |
+--------------------------------------------+
| in Depth                                   |
| is an object oriented programming language |
| is a relational database                   |
| with data structure                        |
+--------------------------------------------+
4 rows in set (0.00 sec)
raja
Published on 25-Sep-2019 10:17:58
Advertisements