在 PostgreSQL 数据库中清理数据
Darryl Reeves, Ph.D.
Industry Assistant Professor, New York University
name | grade | inspection_type | census_tract | ...
--------------------------------+-------+-----------------------------------------+--------------+-----
... | ... | ... | ... | ...
EMPANADAS MONUMENTAL | B | Cycle Inspection / Re-inspection | 26900 | ...
ALPHONSO'S PIZZERIA & TRATTORIA | A | Cycle Inspection / Initial Inspection | 202 | ...
THE SPARROW TAVERN | A | Cycle Inspection / Initial Inspection | 12500 | ...
BURGER KING | A | Cycle Inspection / Re-inspection | 86400 | ...
ASTORIA PIZZA | B | Cycle Inspection / Re-inspection | 6300 | ...
... | ... | ... | ... | ...
name | grade | inspection_type | census_tract | ...
--------------------------------+-------+-----------------------------------------+--------------+-----
... | ... | ... | ... | ...
EMPANADAS MONUMENTAL | B | Cycle Inspection / Re-inspection | 26900 | ...
ALPHONSO'S PIZZERIA & TRATTORIA | A | Cycle Inspection / Initial Inspection | 202 | ...
THE SPARROW TAVERN | A | Cycle Inspection / Initial Inspection | 12500 | ...
BURGER KING | A | Cycle Inspection / Re-inspection | 86400 | ...
ASTORIA PIZZA | B | Cycle Inspection / Re-inspection | 6300 | ...
... | ... | ... | ... | ...
name 的大小写inspection_type 中多余分隔空格census_tract 长度一致 name | grade | inspection_type | census_tract | ...
--------------------------------+-------+-----------------------------------------+--------------+-----
... | ... | ... | ... | ...
EMPANADAS MONUMENTAL | B | Cycle Inspection / Re-inspection | 26900 | ...
ALPHONSO'S PIZZERIA & TRATTORIA | A | Cycle Inspection / Initial Inspection | 202 | ...
THE SPARROW TAVERN | A | Cycle Inspection / Initial Inspection | 12500 | ...
BURGER KING | A | Cycle Inspection / Re-inspection | 86400 | ...
ASTORIA PIZZA | B | Cycle Inspection / Re-inspection | 6300 | ...
... | ... | ... | ... | ...
name | grade | inspection_type | census_tract | ...
--------------------------------+-------+---------------------------------------+--------------+-----
... | ... | ... | ... | ...
Empanadas Monumental | B | Cycle Inspection / Re-inspection | 026900 | ...
Alphonso'S Pizzeria & Trattoria | A | Cycle Inspection / Initial Inspection | 000202 | ...
The Sparrow Tavern | A | Cycle Inspection / Initial Inspection | 012500 | ...
Burger King | A | Cycle Inspection / Re-inspection | 086400 | ...
Astoria Pizza | B | Cycle Inspection / Re-inspection | 006300 | ...
... | ... | ... | ... | ...
INITCAP(input_string) - 修正大小写
SELECT INITCAP('HELLO FRIEND!');
Hello Friend!
REPLACE(input_string, to_replace, replacement) - 将一个文本值替换为另一个
SELECT REPLACE('180 Main Street', 'Street', 'St');
180 Main St
LPAD(input_string, length [, fill_value]) - 在字符串前填充文本
SELECT LPAD('123', 7, 'X');
XXXX123
SELECT
INITCAP(name) as name,
grade,
REPLACE(inspection_type, ' / ', ' / ') as inspection_type,
LPAD(census_tract, 6, '0') as census_tract
FROM
restaurant_inspection;
name | grade | inspection_type | census_tract | ...
--------------------------------+-------+---------------------------------------+--------------+-----
... | ... | ... | ... | ...
Empanadas Monumental | B | Cycle Inspection / Re-inspection | 026900 | ...
Alphonso'S Pizzeria & Trattoria | A | Cycle Inspection / Initial Inspection | 000202 | ...
The Sparrow Tavern | A | Cycle Inspection / Initial Inspection | 012500 | ...
Burger King | A | Cycle Inspection / Re-inspection | 086400 | ...
Astoria Pizza | B | Cycle Inspection / Re-inspection | 006300 | ...
... | ... | ... | ... | ...
在 PostgreSQL 数据库中清理数据