ROUND(number, decimal_places)
ROUND(5555.6666, 2) → 5555.67
ROUND(5555.6666, -2) → 5600 -- 負數:小數點左側第 2 位
ROUND(5555.6666) → 5556 -- 預設 0 位
對應 TRUNC 直接截斷不四捨五入。
LPAD(string, length, [pad_string]) -- 預設 pad_string = ' '
RPAD(string, length, [pad_string])
LPAD('tech', 7) → ' tech'
LPAD('tech', 2) → 'te' -- 截斷
LPAD('tech', 8, '0') → '0000tech'
RPAD('tech', 8, '0') → 'tech0000'
RPAD('tech', 16, 'z') → 'tech on the netz' -- 原長15,補1個z
常用於:流水號補零、報表對齊、固定長度介面格式化。
機制:從左/右邊開始,逐字元比對 trim_set 中的**任一字元**,符合即移除,直到遇到不在集合中的字元停止。**不是**匹配整個字串。
LTRIM('1092002081100058424', '109') → '2002081100058424'
-- 依次移除左側 '1','0','9' 任意組合,直到遇到 '2' 停止
LTRIM('1091000000002671251', '109') → '2671251'
LTRIM('1000000002671251', '1') → '000000002671251'
LTRIM('1000000002671251', '10') → '2671251' -- '1'和'0'都在集合中
RTRIM('wfrqqww', 'qq') → 'wfr'
-- 從右移除 'q' 或 'w',直到遇到 'r' 停止
INSTR(string, substring, [position], [occurrence])
INSTR('syranmo', 's') → 1
INSTR('syranmo', 'ra') → 3
INSTR('syran mo', 'a', 1, 2) → 0 -- 第2次出現不存在
INSTR('syranmo', 'a', -1, 1) → 6 -- 從右往左找第一個 'a'
position 負數表示從右向左計算起始位置,但搜尋方向仍左→右。SUBSTR(string, start_position, [length])
SUBSTR('Hello World', 0, 1) → 'H' -- 0 視同 1
SUBSTR('Hello World', 1, 1) → 'H'
SUBSTR('Hello World', 2, 4) → 'ello'
SUBSTR('Hello World', -3, 3) → 'rld' -- 負數:從右數第3字元起取3個
SUBSTR('Hello World', -5) → 'World' -- 省略 length 取至末尾
對應位元組語義版本:SUBSTRB、SUBSTRC、SUBSTR2、SUBSTR4。
TRIM(col) = LTRIM(RTRIM(col))。SUBSTR(col, -N)。SUBSTR(col, 1, INSTR(col, ',')-1) 取第一欄。RPAD(SUBSTR(mobile,1,3), 11, '*') → 138********。字串函數是 SQL/ETL 處理的核心工具,熟記 LTRIM/RTRIM 的「字元集匹配」而非「字串匹配」語義,能避免 90% 的修剪邏輯錯誤。