9/9 每日一題(SQL 將名字重新改成首字大寫,其餘小寫的規則)
Table: Users
+----------------+---------+ | Column Name | Type | +----------------+---------+ | user_id | int | | name | varchar | +----------------+---------+ user_id is the primary key (column with unique values) for this table. This table contains the 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 by user_id.
The result format is in the following example.
Example 1:
Input: Users table: +---------+-------+ | user_id | name | +---------+-------+ | 1 | aLice | | 2 | bOB | +---------+-------+ Output: +---------+-------+ | user_id | name | +---------+-------+ | 1 | Alice | | 2 | Bob | +---------+-------+
# Write your MySQL query statement below
SELECT user_id,CONCAT(UPPER(LEFT(name,1)),LOWER(RIGHT(name,LENGTH(name)-1))) AS name
FROM Users
Order by user_id
#使用concat() 將大寫的首字 以及剩餘的小寫字母合併
#使用LEFT(name,1) 回傳欄位name左邊數來第1個字母,也就是字首
#使用RIGHT(name,LENGTH(name)-1) 回傳欄位name從右邊數來到倒數第2個字母,也就是除了首字以外的所有字母
-----------------------------------
# Write your MySQL query statement below
select user_id, CONCAT(UPPER(SUBSTRING(name,1,1)), LOWER(SUBSTRING(name,2))) AS name
from users
order by user_id標籤: leetcode

0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁