본문 바로가기
Algorithm/LeetCode_SQL

[LeetCode_SQL] 1667. Fix Names in a Table - Oracle & MySQL

by yunamom 2022. 8. 13.
728x90
300x250

Table: Users

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| user_id        | int     |
| name           | varchar |
+----------------+---------+
user_id is the primary key for this table.
This table contains the ID and the name of the user. The name consists of only lowercase and uppercase characters.

 

Write an SQL query 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 query 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   |
+---------+-------+

 

1667. Fix Names in a Table

users 테이블의 name 컬럼 데이터 첫글자를 대문자, 그외는 소문자로 변환하여 출력하기

예: boB -> Bob

 

 Oracle

UPPER(문자열) : 괄호 안 문자 데이터를 모두 대문자로 변환

LOWER(문자열) : 괄호 안 문자 데이터를 모두 소문자로 변환

INITCAP(문자열) : 괄호 안 문자 데이터 중 첫 글자는 대문자로, 나머지는 소문자로 변환

 

SELECT user_id,
    INITCAP(name) name
FROM users
ORDER BY user_id;

 

 MySQL 

SELECT LEFT('abcdef', 3); 왼쪽에서부터 3개를 제외한 나머지 삭제

SELECT RIGHT('abcdef', 3);

SELECT MID('abcdefg', 2);  2번째에서부터 마지막까지 출력 ( bcdefg )

SELECT SUBSTRING('abcdefg', 2, 3);  2번째에서부터 3번째 까지 출력 ( bcd )

 

SELECT CONCAT('[', ' abc ' , ']');  공백제거 없이 연결

SELECT CONCAT('[', LTRIM(' abc ') , ']');  왼쪽공백 제거 후 연결

SELECT CONCAT('[', RTRIM(' abc ') , ']');

SELECT CONCAT('[', TRIM(' abc ') , ']');  양쪽공백 제거 후 연결

 

 

SELECT UCASE('acDDefg');  대문자 변환 

SELECT UPPER('acDDefg');  대문자 변환

 

SELECT LCASE('acDDefg');  소문자 변환

SELECT LOWER('acDDefg');  소문자 변환

 

SELECT REVERSE('acDDefg'); → REVERSE, 반전

SELECT 
    user_id,
    CONCAT(UCASE(LEFT(name, 1)),LCASE(SUBSTRING(name, 2))) 
    AS name 
FROM users 
ORDER BY user_id;

 

 

728x90
300x250

코드