

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Update a table in MySQL and display only the initials name in a new column
To get the initial, use the concept of left() along with substring_index().
Let us create a table −
mysql> create table demo13 −> ( −> full_name varchar(100), −> short_name varchar(20) −> ); Query OK, 0 rows affected (1.18 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo13(full_name) values('John Smith'); Query OK, 1 row affected (0.27 sec) mysql> insert into demo13(full_name) values('David Miller'); Query OK, 1 row affected (0.13 sec) mysql> insert into demo13(full_name) values('Chris Brown'); Query OK, 1 row affected (0.28 sec)
Display records from the table using select statement −
mysql> select *from demo13;
This will produce the following output −
+--------------+------------+ | full_name | short_name | +--------------+------------+ | John Smith | NULL | | David Miller | NULL | | Chris Brown | NULL | +--------------+------------+ 3 rows in set (0.00 sec)
Following is the query to update table and get the initials name −
mysql> update demo13 −> set short_name= concat( −> left(full_name, 1), −> left(substring_index(full_name, ' ', −1), 1) −> ); Query OK, 3 rows affected (0.14 sec) Rows matched: 3 Changed: 3 Warnings: 0
Display records from the table using select statement −
mysql> select *from demo13;
This will produce the following output −
+--------------+------------+ | full_name | short_name | +--------------+------------+ | John Smith | JS | | David Miller | DM | | Chris Brown | CB | +--------------+------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- Update only a single column in a MySQL table and increment on the basis of a condition
- Update only a single column value in MySQL
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- Display distinct column name in MySQL
- Selecting and displaying only some rows from a column in a MySQL table
- Find and display duplicate values only once from a column in MySQL
- Best way to update a single column in a MySQL table?
- How to round MySQL column with float values and display the result in a new column?
- Program to find the initials of a name in C++
- MySQL query to group by names and display the count in a new column
- Querying only the field name and display only the id in MongoDB?
- Display MySQL Database, Table, and Column Information
- Update a column based on another MySQL table’s column
- Multiply values of two columns and display it a new column in MySQL?
- Set the NULL values to 0 and display the entire column in a new column with MySQL SELECT
Advertisements