การทำความสะอาด string

การรายงานผลด้วย SQL

Tyler Pernes

Learning & Development Consultant

String ที่ไม่สอดคล้องกัน

+---------------------+
| country             |
|---------------------|
| US                  |
| U.S.                |
| US (United States)  |
| us                  |
|   US                |
+---------------------+
การรายงานผลด้วย SQL

ฟังก์ชัน string

การรายงานผลด้วย SQL

ฟังก์ชัน string

การรายงานผลด้วย SQL

การแทนที่หรือลบอักขระ

+------------+--------+
| 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      |
+-----------------+--------+
การรายงานผลด้วย SQL

ฟังก์ชัน string

การรายงานผลด้วย SQL

การแยกส่วนของ string

+---------------------+--------+
| 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      |
+------------------+--------+
การรายงานผลด้วย SQL

ฟังก์ชัน string

การรายงานผลด้วย SQL

การเปลี่ยนตัวพิมพ์

+----------+--------+
| 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      |
+------------------+--------+
การรายงานผลด้วย SQL

ฟังก์ชัน string

การรายงานผลด้วย SQL

การตัดช่องว่างส่วนเกิน

+----------+--------+
| 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      |
+------------------+--------+
การรายงานผลด้วย SQL

การซ้อนฟังก์ชัน

original_table
+---------------------+
| country             |
|---------------------|
| US                  |
| U.S.                |
| US (United States)  |
| us                  |
|   US                |
+---------------------+
REPLACE(country,'.','')
TRIM(country)
LEFT(country,2)
UPPER(country)
การรายงานผลด้วย SQL

ทำทีละขั้นตอน

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

คิวรีสุดท้าย:

SELECT UPPER(LEFT(TRIM(REPLACE(country,'.','')),2)) AS country_cleaned
FROM original_table
GROUP BY country_cleaned;
การรายงานผลด้วย SQL

ลำดับการซ้อนฟังก์ชันสำคัญมาก!

SELECT TRIM(REPLACE(UPPER(LEFT(country,2)),'.','')) AS country_cleaned
FROM original_table
GROUP BY country_cleaned;
+-------------------+
| country_cleaned   |
|-------------------|
| US                |
| U                 |
|                   |
+-------------------+
การรายงานผลด้วย SQL

เอกสารอ้างอิงฟังก์ชัน string

การรายงานผลด้วย SQL

มาฝึกกันเถอะ!

การรายงานผลด้วย SQL

Preparing Video For Download...