TRANSLATE ( 'string' , 'from_string' , 'to_string' )
【参数】string 原字符串,from_string 待转换的每个字符,to_string 将转换成的每个字符
【功能】将string中每个包含 from_string 的字符转换成 to_string 中对应位置字符
【例子】
select
translate
(
'WUHEN'
,
'WAN'
,
'LB'
)
from
dual;
TRANSLATE('WUHEN','WAN','LB')
-----------------------------
LUHE
from_string 为
WAN
,string为
WUHEN,string中的W字符转换为对应的字符 '1';'A'转换为'B',但没有字符A,不做转换;'N'to_string中没有对应需去除,'WUHEN'中的其他字符不变。
W--L
U--U
H--H
E--E
N--
【实际应用】
环境:oracle9i
目标:判断手机格式是否正确
sql:
select
*
from
table
WHERE
length
(tel)=
11
AND
SUBSTR
(
tel
,
1
,
2
)
IN
(
'13'
,
'14'
,
'15'
,
'18'
)
and
translate
(
tel
,
'$1234567890'
,
'$'
)
is
null