How to add a specific character to any empty space in MySQL table values?


For this, use REPLACE() function and replace empty space with the character. Let us first create a table −

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

Example

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Introduction to MySQL');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Java in depth');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values('Data Structure and Algorithm');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+------------------------------+
| Subject |
+------------------------------+
| Introduction to MySQL |
| Java in depth |
| Data Structure and Algorithm |
+------------------------------+
3 rows in set (0.00 sec)

Following is the query to replace empty space −

mysql> SELECT *,REPLACE(Subject,' ','_') as Subject_Name from DemoTable;

Output

+-------------------------------+------------------------------+
| Subject | Subject_Name |
+-------------------------------+------------------------------+
| Introduction to MySQL | Introduction_to_MySQL |
| Java in depth | Java_in_depth |
| Data Structure and Algorithm | Data_Structure_and_Algorithm |
+-------------------------------+------------------------------+
3 rows in set (0.00 sec)

Updated on: 09-Mar-2020

272 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements