Adding dash between spaces in field name in MySQL?


You can use REPLACE() for this. Let us first create a table −

mysql> create table DemoTable1625
    -> (
    -> FullName varchar(20)
    -> );
Query OK, 0 rows affected (0.68 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1625 values('John Doe');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1625 values('Adam Smith');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1625 values('John Smith');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1625 values('Carol Taylor');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1625;

This will produce the following output −

+--------------+
| FullName     |
+--------------+
| John Doe     |
| Adam Smith   |
| John Smith   |
| Carol Taylor |
+--------------+
4 rows in set (0.00 sec)

Following is the query to add dash between spaces in field name −

mysql> update DemoTable1625 set FullName=replace(FullName,' ','-');
Query OK, 4 rows affected (0.15 sec)
Rows matched: 4  Changed: 4 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1625;

This will produce the following output −

+--------------+
| FullName     |
+--------------+
| John-Doe     |
| Adam-Smith   |
| John-Smith   |
| Carol-Taylor |
+--------------+
4 rows in set (0.00 sec)
raja
Published on 08-Nov-2019 11:16:51
Advertisements