Huruf dan Spasi

Analisis Data Eksploratif di SQL

Christina Maimone

Data Scientist

Mengubah huruf besar/kecil

SELECT lower('aBc DeFg 7-');
abc defg 7-
SELECT upper('aBc DeFg 7-');
ABC DEFG 7-
Analisis Data Eksploratif di SQL

Perbandingan tidak peka huruf besar/kecil

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)
Analisis Data Eksploratif di SQL

Pencarian tidak peka huruf besar/kecil

-- Menggunakan LIKE

SELECT * 
  FROM fruit
 -- "apple" ada dalam nilai
 WHERE fav_fruit LIKE '%apple%';
 customer | fav_fruit 
----------+-----------
      349 | apple
      703 |  apple
      313 | apple
      418 | apple
      754 | apple  
(5 rows)
-- Menggunakan ILIKE

SELECT * 
  FROM fruit
 -- ILIKE untuk tidak peka huruf besar/kecil
 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)
Analisis Data Eksploratif di SQL

Hati-hati!

SELECT fruit 
  FROM fruit2;
   fruit 
------------
 apple
 banana
 pineapple
 grapefruit
 grapes
SELECT fruit 
  FROM fruit2
 WHERE fruit LIKE '%apple%';
   fruit 
------------
 apple
 pineapple
Analisis Data Eksploratif di SQL

Memangkas spasi

SELECT trim(' abc ');
  • trim atau btrim: kedua sisi
    • trim(' abc ') = 'abc'
  • rtrim: sisi kanan
    • rtrim(' abc ') = ' abc'
  • ltrim: sisi kiri
    • ltrim(' abc ') = 'abc '
Analisis Data Eksploratif di SQL

Memangkas karakter lain

SELECT trim('Wow!', '!');
 Wow
SELECT trim('Wow!', '!wW');
 o
Analisis Data Eksploratif di SQL

Menggabungkan fungsi

SELECT trim(lower('Wow!'), '!w');
 o
Analisis Data Eksploratif di SQL

Rapikan teks berantakan!

Analisis Data Eksploratif di SQL

Preparing Video For Download...