SQL Serise Part V (Data Cleaning)

LEFT & RIGHT (for column)

# choose the col string you want to separate
SELECT LEFT(col, number) AS new_col # col=> string column, number=> separate index position
FROM TABLE;

SELECT RIGHT(col, number) AS new_col
FROM TABLE;

# Sample:
#    if we want to count the `name` start with 'a' character
SELECT SUM(new_name) AS n_name
FROM (SELECT name, CASE WHEN LEFT(name, 1)='a'
                   THEN 1 ELSE 0 END AS new_name 
                   FROM TABLE) AS t1;

POSITION, STRPOS & SUBSTR

# POSITION, STRPOS: provides the position of a string counting from the left
# ATTENTION: both them are case sensitive
POSITION('target_string' IN col)
STRPOS(col, 'target_string')

# If you want separate the string, use LEFT or RIGHT and POSITION or STRPOS
...
LEFT(col, POSITION('target_string' IN col) -1 ) AS new_col # -1 is to substracting the target_string
...

LOWER, UPPER

# force every character in a string to become lowercase(uppercase)
LOWER(col)
UPPER(col)

CONCAT & ||

# CONCAT & ||: combines values from several columns into one column
...
CONCAT(a, 'space mark(or nothing)', b) AS new_col
a || 'space mark(or nothing)' || b AS new_col
...

CAST

# Allows us to change columns from one data type to another
# change float to int:
CAST(25.6 AS int) => 25
# change string to date:
CAST(year || '-' || month || '-' || day AS date) => 2018-08-21

COALESCE

# Returns the first non-null value passed for each row
COALESCE(col, 'Nothing here') AS show_non-null_col => if col is null, then will show 'Nothing here'
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類(lèi): pyspark.sql...
    mpro閱讀 9,919評(píng)論 0 13
  • 目標(biāo):能夠?qū)υ紨?shù)據(jù)進(jìn)行清理,并獲得適合分析的整潔數(shù)據(jù)。 清理和重新整理混亂的數(shù)據(jù)。 將列轉(zhuǎn)換為不同的數(shù)據(jù)類(lèi)型。 ...
    夏威夷的芒果閱讀 494評(píng)論 0 0
  • 開(kāi)場(chǎng)阿斯加德人遭到了滅霸的襲擊,他們對(duì)附近發(fā)出了求救訊號(hào),抵抗已經(jīng)失敗,烏木喉跨過(guò)死者,受傷的海姆達(dá)爾躺在地上,洛...
    Off_time閱讀 892評(píng)論 0 0
  • 打卡練字中,目前還是一只小菜鳥(niǎo)。。。 歡迎各路大神指教!
    陳硯v閱讀 393評(píng)論 0 0
  • 我曾經(jīng)是一個(gè)很愛(ài)抱怨的人,總覺(jué)得老師講課的方法有問(wèn)題,布置的作業(yè)沒(méi)有意義,不如我自己自學(xué)來(lái)的痛快。直到暑假的夏令營(yíng)...
    你好啊我叫露娜閱讀 359評(píng)論 0 6

友情鏈接更多精彩內(nèi)容