パターンマッチング

SQL Server データベースでのデータクレンジング

Miriam Antona

Software Engineer

パターンマッチング - 導入

SELECT * FROM series
| id  | name            | contact_number | ... |
|-----|-----------------|----------------|-----|
| 1   | Adventure Time  | 555-906-8845   | ... |
| 2   | Dexter          | 555-156-8845   | ... |
| 3   | Futurama        | 555-210-9951   | ... |
| 4   | Game of Thrones | 555-543-6641   | ... |
| ... | ...             | ...            | ... |
  • 有効な番号: ###-###-####
    • 1桁目と4桁目は2〜9
    • それ以外は0〜9
  • 無効な番号の例: 555-156-8845
SQL Server データベースでのデータクレンジング

パターンマッチング - 導入

  • SQL Server は完全な正規表現機能を提供しません。
  • SQL Server では LIKE でパターン照合が可能です。
  • 完全な正規表現が必要な場合は、拡張機能を作成・導入します。
SQL Server データベースでのデータクレンジング

パターンマッチング - LIKE

  • 文字列が指定パターンに一致するか判定します
SQL Server データベースでのデータクレンジング

パターンマッチング - LIKE

  • 文字列が指定パターンに一致するか判定します
ワイルドカード 説明
% 0文字以上の任意の文字列 WHERE contact_number LIKE '555-%'
_ (アンダースコア) 任意の1文字 WHERE contact_number LIKE '___-___-____'
[] 指定範囲・集合内の任意の1文字 WHERE contact_number LIKE '[2-9][0-9][0-9]-[2-9][0-9][0-9]-[0-9][0-9][0-9][0-9]
[^] 指定範囲・集合外の任意の1文字 WHERE contact_number LIKE '[^2-9]'
SQL Server データベースでのデータクレンジング

パターンマッチング - % の例

SELECT name, contact_number
FROM series
WHERE contact_number LIKE '555%'
| name            | contact_number |
|-----------------|----------------|
| Adventure Time  | 555-906-8845   |
| Dexter          | 555-156-8845   |
| Futurama        | 555-210-9951   |
| Game of Thrones | 555-abc-6641   |
| ...             | ...            |
SQL Server データベースでのデータクレンジング

パターンマッチング - % の例

SELECT 
    name, 
    contact_number
FROM series
WHERE contact_number NOT LIKE '555%'
| name            | contact_number |
|-----------------|----------------|
| The Good Doctor | 000-930-1274   |
SQL Server データベースでのデータクレンジング

パターンマッチング - [](角かっこ)の例

SELECT 
    name, 
    contact_number
FROM series
WHERE contact_number LIKE '[2-9][0-9][0-9]-[2-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'
| name           | contact_number |
|----------------|----------------|
| Adventure Time | 555-906-8845   |
| Futurama       | 555-210-9951   |
| Homeland       | 555-985-6314   |
| Westworld      | 555-456-1234   |
| ...            | ...            |
SQL Server データベースでのデータクレンジング

パターンマッチング - [](角かっこ)の例

SELECT 
    name, 
    contact_number
FROM series
WHERE contact_number NOT LIKE '[2-9][0-9][0-9]-[2-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'
| name            | contact_number |
|-----------------|----------------|
| Dexter          | 555-156-8845   |
| Game of Thrones | 555-abc-6641   |
| The Good Doctor | 000-930-1274   |
SQL Server データベースでのデータクレンジング

Ayo berlatih!

SQL Server データベースでのデータクレンジング

Preparing Video For Download...