Làm sạch chuỗi

Lập báo cáo trong SQL

Tyler Pernes

Learning & Development Consultant

Chuỗi lộn xộn

+---------------------+
| country             |
|---------------------|
| US                  |
| U.S.                |
| US (United States)  |
| us                  |
|   US                |
+---------------------+
Lập báo cáo trong SQL

Hàm chuỗi

Lập báo cáo trong SQL

Hàm chuỗi

Lập báo cáo trong SQL

Thay thế hoặc xóa ký tự

+------------+--------+
| country    | points |
|------------|--------|
| US         | 5      |
| U.S.       | 3      |
+------------+--------+
SELECT REPLACE(country,'.','') AS country_cleaned, SUM(points) as points
FROM original_table
GROUP BY country_cleaned;
+-----------------+--------+
| country_cleaned | points |
|-----------------|--------|
| US              | 8      |
+-----------------+--------+
Lập báo cáo trong SQL

Hàm chuỗi

Lập báo cáo trong SQL

Phân tích chuỗi

+---------------------+--------+
| country             | points |
|---------------------|--------|
| US                  | 5      |
| US (United States)  | 1      |
+---------------------+--------+
SELECT LEFT(country,2) AS country_cleaned, SUM(points) as points
FROM original_table
GROUP BY country_cleaned;
+------------------+--------+
| country_cleaned  | points |
|------------------|--------|
| US               | 6      |
+------------------+--------+
Lập báo cáo trong SQL

Hàm chuỗi

Lập báo cáo trong SQL

Đổi chữ hoa/thường

+----------+--------+
| country  | points |
|----------|--------|
| US       | 5      |
| us       | 4      |
+----------+--------+
SELECT UPPER(country) AS country_cleaned, SUM(points) as points
FROM original_table
GROUP BY country_cleaned;
+------------------+--------+
| country_cleaned  | points |
|------------------|--------|
| US               | 9      |
+------------------+--------+
Lập báo cáo trong SQL

Hàm chuỗi

Lập báo cáo trong SQL

Loại bỏ khoảng trắng thừa

+----------+--------+
| country  | points |
|----------|--------|
| US       | 5      |
|   US     | 2      |
+----------+--------+
SELECT TRIM(country) AS country_cleaned, SUM(points) as points
FROM original_table
GROUP BY country_cleaned;
+------------------+--------+
| country_cleaned  | points |
|------------------|--------|
| US               | 7      |
+------------------+--------+
Lập báo cáo trong SQL

Lồng nhiều hàm

original_table
+---------------------+
| country             |
|---------------------|
| US                  |
| U.S.                |
| US (United States)  |
| us                  |
|   US                |
+---------------------+
REPLACE(country,'.','')
TRIM(country)
LEFT(country,2)
UPPER(country)
Lập báo cáo trong SQL

Làm từng bước

REPLACE(country,'.','')
TRIM(REPLACE(country,'.',''))
LEFT(TRIM(REPLACE(country,'.','')),2)
UPPER(LEFT(TRIM(REPLACE(country,'.','')),2))

Truy vấn cuối cùng:

SELECT UPPER(LEFT(TRIM(REPLACE(country,'.','')),2)) AS country_cleaned
FROM original_table
GROUP BY country_cleaned;
Lập báo cáo trong SQL

Thứ tự lồng hàm rất quan trọng!

SELECT TRIM(REPLACE(UPPER(LEFT(country,2)),'.','')) AS country_cleaned
FROM original_table
GROUP BY country_cleaned;
+-------------------+
| country_cleaned   |
|-------------------|
| US                |
| U                 |
|                   |
+-------------------+
Lập báo cáo trong SQL

Tài liệu về hàm chuỗi

Lập báo cáo trong SQL

Ayo berlatih!

Lập báo cáo trong SQL

Preparing Video For Download...