How to lowercase the entire string keeping the first letter in uppercase with MySQL?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Name varchar(100)
   -> );
Query OK, 0 rows affected (1.32 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('JOhn');
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values('CHRIS');
Query OK, 1 row affected (0.16 sec)

mysql> insert into DemoTable values('DAVID');
Query OK, 1 row affected (0.15 sec)

mysql> insert into DemoTable values('RObert');
Query OK, 1 row affected (0.21 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+--------+
| Name   |
+--------+
| JOhn   |
| CHRIS  |
| DAVID  |
| RObert |
+--------+
4 rows in set (0.00 sec)

Following is the query to lowercase the entire string keeping the first letter in uppercase in MySQL −

mysql> update DemoTable
   -> set Name= CONCAT(UCASE(LEFT(Name, 1)), LCASE(SUBSTRING(Name, 2)));
Query OK, 4 rows affected (0.30 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Let us check all records from the table once again.

mysql> select *from DemoTable;

Output

+--------+
| Name   |
+--------+
| John   |
| Chris  |
| David  |
| Robert |
+--------+
4 rows in set (0.00 sec)
raja
Published on 09-Jul-2019 15:20:57
Advertisements