大小寫與空白

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 中的探索式資料分析

去除空白(trim)

SELECT trim(' abc ');
  • trimbtrim:兩端
    • trim(' abc ') = 'abc'
  • rtrim:右端
    • rtrim(' abc ') = ' abc'
  • ltrim:左端
    • 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...