How can we capitalize only first letter of a string with the help of MySQL function/s?


Actually, there is no single function in MySQL to capitalize only first letter of the string. We need to use nesting of functions and for this case, we can use UPPER() and LOWER() with SUBSTRING() functions. To understand it, we are using data, given as below, from ‘emp_tbl’.

mysql> Select * from emp_tbl;
+----+----------------+
| Id | Name           |
+----+----------------+
| 1  | rahul singh    |
| 2  | gaurav kumar   |
| 3  | yashpal sharma |
| 4  | krishan kumar  |
| 5  | kuldeep rai    |
| 6  | munish nayak   |
+----+----------------+
6 rows in set (0.00 sec)

We can see from the above result set that the first character of name string is in small letters. The following query will capitalize the first letter of string −

mysql> Select CONCAT(UPPER(SUBSTRING(name,1,1)),LOWER(SUBSTRING(name,2))) AS Name from emp_tbl;
+----------------+
| Name           |
+----------------+
| Rahul singh    |
| Gaurav kumar   |
| Yashpal sharma |
| Krishan kumar  |
| Kuldeep rai    |
| Munish nayak   |
+----------------+
6 rows in set (0.00 sec)

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 14-Sep-2023

23K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements