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 |
테이블에 있는 이름 컬럼에 데이터를 첫글자만 대문자로 바꾸고, 나머지는 소문자로 표시하여 조회하는 문제.
select user_id
,concat(upper(substring(name, 1, 1))
,lower(substring(name, 2))) as name
from users
order by user_id;
upper(substring(name, 1, 1) :
substring 을 사용하여 이름의 첫글자, 한개의 글자만 뽑아낸다. 다음으로 upper 를 사용하여 뽑아낸 글자를 대문자로 바꿔준다.
lower(substring(name, 2)) :
마찬가지로 substring 을 사용하여 두번째 문자부터 끝의 문자를 뽑아낸다. lower 를 사용하여 뽑아낸 글자를 소문자로 바꿔준다.
concat :
대문자, 소문자로 변환한 글자를 concate 를 사용하여 두 글자를 합쳐준다.