| 函數 | 單位 | 說明 |
|---|---|---|
LENGTH(str) | 字元數 | 不論字元集,一個中文算 1 |
LENGTHB(str) | 位元組數 | 視字元集而定:AL32UTF8 中文=3 bytes,ZHS16GBK 中文=2 bytes |
-- 移除 depre_name 尾端 15 個字元(保留前 length-16 字元) select t.depre_name, substr(t.depre_name, 1, length(t.depre_name) - 16) from table t where t.del_flag = '0';
關鍵點:
SUBSTR(str, 1, LENGTH(str) - N):從第 1 字元開始,取長度減 NSUBSTR 起始位置從 1 開始(非 0)LENGTHB 需對應字元集位元組數-- 移除所有點號
select replace('de.5d', '.', '') from dual;
-- 結果:de5d
-- 通用語法
select replace(欄位名, '指定字元', '替換字元') from 表名;
-- 替換字元為空字串 '' 即為刪除
-- 移除多種特殊字元(巢狀 REPLACE) select replace(replace(replace(col, chr(9), ''), chr(10), ''), chr(13), '') from t; -- 移除 Tab、換行、CR -- 使用 TRANSLATE 一次對應多字元(11g+) select translate(col, 'x' || chr(9) || chr(10) || chr(13), 'x') from t; -- 將指定字元對應為空(需佔位符 x)
COMMIT,避免 Undo 表空間暴增create index idx_t_clean on t(replace(col, '.', ''))LENGTHB 結果為字元數 × 3(中文),截取邏輯需留意