| 函數 | 語法 | 範例 | 結果 |
|---|---|---|---|
| LEFT | LEFT(str, len) | LEFT('foobarbar', 5) | 'fooba' |
| RIGHT | RIGHT(str, len) | RIGHT('foobarbar', 5) | 'arbar' |
中文同樣按字元:LEFT('我是中文', 2) → '我是'。
SUBSTRING(str, pos) -- 從 pos 到結束 (1-based)
SUBSTRING(str, pos, len) -- 從 pos 取 len 字元
SUBSTRING(str, -pos) -- 從倒數 pos 到結束
SUBSTRING(str, -pos, len) -- 從倒數 pos 取 len
-- 標準 SQL 寫法
SUBSTRING(str FROM pos FOR len)
SUBSTRING('Quadratically', 5) -- 'ratically'
SUBSTRING('foobarbar', 4) -- 'barbar'
SUBSTRING('Quadratically', 5, 6) -- 'ratica'
SUBSTRING('Sakila', -3) -- 'ila'
SUBSTRING('Sakila', -5, 3) -- 'aki'
SUBSTRING_INDEX(str, delim, count)
-- count > 0:從左數第 count 個 delim 之前
-- count < 0:從右數第 |count| 個 delim 之後
SUBSTRING_INDEX('blog.chinabyte.com', '.', 2) -- 'blog.chinabyte'
SUBSTRING_INDEX('blog.chinabyte.com', '.', -2) -- 'chinabyte.com'
找不到 delim → 回傳原字串。
INSTR('foobarbar', 'bar') -- 4 (1-based)
INSTR('xbar', 'foobar') -- 0