Home > Database > Mysql Tutorial > 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
Release: 2023-09-21 16:05:17
forward
813 people have browsed it

如何借助 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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template