Select*fromemp_tbl;+----+----------------+|Id|Name |+----+---------- -----"/> Select*fromemp_tbl;+----+----------------+|Id|Name |+----+---------- -----">

Home  >  Article  >  Database  >  How to capitalize only the first letter of a string with the help of MySQL function?

How to capitalize only the first letter of a string with the help of MySQL function?

PHPz
PHPzforward
2023-09-21 16:05:17728browse

如何借助 MySQL 函数仅将字符串的第一个字母大写?

In fact, there is no function in MySQL that can only capitalize the first letter of a string. We need to use function nesting, for this case we can use UPPER() and LOWER() with SUBSTRING() function. To understand it, we use data from "emp_tbl" as shown below.

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)

As can be seen from the above result set, the first character of the name string is a lowercase letter. The following query capitalizes the first letter of a 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)

The above is the detailed content of How to capitalize only the first letter of a string with the help of MySQL function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete