ケースとスペース

SQLによる探索的データ分析

Christina Maimone

Data Scientist

ケース変換

SELECT lower('aBc DeFg 7-');
abc defg 7-
SELECT upper('aBc DeFg 7-');
ABC DEFG 7-
SQLによる探索的データ分析

大文字小文字を区別しない比較

SELECT * 
  FROM fruit;
 customer | fav_fruit 
----------+-----------
      349 | apple            <- #1
      874 | Apple            <- #2
      703 |  apple           <- #3
      667 | bannana
      622 |  banana
      387 | BANANA
      300 | APPLES           <- #4
      313 | apple            <- #5
      499 | banana
      418 | apple            <- #6
      841 | BANANA
      300 | APPLE            <- #7
      754 | apple            <- #8 
(13 rows)
SELECT * 
  FROM fruit
 WHERE lower(fav_fruit)='apple';
 customer | fav_fruit 
----------+-----------
      349 | apple
      874 | Apple
      313 | apple
      418 | apple
      300 | APPLE
(5 rows)
SQLによる探索的データ分析

大文字・小文字を区別しない検索

-- Using LIKE

SELECT * 
  FROM fruit
 -- "apple" in value
 WHERE fav_fruit LIKE '%apple%';
 customer | fav_fruit 
----------+-----------
      349 | apple
      703 |  apple
      313 | apple
      418 | apple
      754 | apple  
(5 rows)
-- Using ILIKE

SELECT * 
  FROM fruit
 -- ILIKE for case insensitive
 WHERE fav_fruit ILIKE '%apple%';
 customer | fav_fruit 
----------+-----------
      349 | apple
      874 | Apple
      703 |  apple
      300 | APPLES
      313 | apple
      418 | apple
      300 | APPLE
      754 | apple  
(8 rows)
SQLによる探索的データ分析

注意!

SELECT fruit 
  FROM fruit2;
   fruit 
------------
 apple
 banana
 pineapple
 grapefruit
 grapes
SELECT fruit 
  FROM fruit2
 WHERE fruit LIKE '%apple%';
   fruit 
------------
 apple
 pineapple
SQLによる探索的データ分析

スペースのトリミング

SELECT trim(' abc ');
  • trim または btrim: b両端
    • trim(' abc ') = 'abc'
  • rtrim: right end
    • rtrim(' abc ') = ' abc'
  • ltrim: left start
    • ltrim(' abc ') = 'abc '
SQLによる探索的データ分析

他の値をトリミングする

SELECT trim('Wow!', '!');
 Wow
SELECT trim('Wow!', '!wW');
 o
SQLによる探索的データ分析

()関数の組み合わせ

SELECT trim(lower('Wow!'), '!w');
 o
SQLによる探索的データ分析

乱雑なテキストを整理しよう!

SQLによる探索的データ分析

Preparing Video For Download...