Capitalization Conversion Conundrum in MySQL
When it comes to manipulating strings in MySQL, one common task is capitalizing the first letter of each word. To achieve this, a TSQL equivalent is available in MySQL, but with slight modifications.
Capitalizing the First Letter
The TSQL syntax for capitalizing the first letter is:
UPDATE tb_Company SET CompanyIndustry = UPPER(LEFT(CompanyIndustry, 1)) + SUBSTRING(CompanyIndustry, 2, LEN(CompanyIndustry))
To apply this in MySQL, replace the operator with the CONCAT() function:
UPDATE tb_Company SET CompanyIndustry = CONCAT(UCASE(LEFT(CompanyIndustry, 1)), SUBSTRING(CompanyIndustry, 2))
This effectively achieves the same result as the TSQL equivalent. For example, "hello" becomes "Hello", "wOrLd" becomes "WOrLd", and "BLABLA" remains "BLABLA".
Optional: Capitalizing First Letter and Lowercasing Others
To simultaneously capitalize the first letter and lowercase the remaining characters, use the LCASE function:
UPDATE tb_Company SET CompanyIndustry = CONCAT(UCASE(LEFT(CompanyIndustry, 1)), LCASE(SUBSTRING(CompanyIndustry, 2)))
Note that MySQL uses UCASE and UPPER interchangeably for uppercasing.
The above is the detailed content of How to Capitalize the First Letter of Each Word in MySQL?. For more information, please follow other related articles on the PHP Chinese website!