Leetcode SQL 50 -- 1667. Fix Names in a Table
Question
Table name: Users
Column Name | Type |
---|---|
user_id | int |
name | varchar |
This table contains the user_id
and the name
of the user. The name
consists of only lowercase and uppercase characters.
Write a solution to fix the names so that only the first character is uppercase and the rest are lowercase.
Return the result table ordered byuser_id
.
The result format is in the following example.
Explaination
We can split the tasks into three part.
- Split the name into first character and the remaining characters →
SUBSTRING()
- Modify them into upper case and lower case respectively →
UPPER()
andLOWER()
- Order the result by
user_id
→ORDER BY
SQL SUBSTRING() Function
For extracting characters from a string, we can use the function SUBSTRING(<string>, start, end)
.
For example,