Oracle的字符替换函数translate用法

Oracle的字符替换函数translate用法  

2009-07-15 18:48:51|  分类: JAVA笔记|字号 订阅

Oracle提供了一个字符替换函数translate,不同于replace函数的是,translate函数是字符级别的替换,而不是字符串的替换。

其语法如下:

TRANSLATE ( expr , from_string , to_string )

简单的说就是对expr内容,用to_string中的字符逐一替换from_string 中的字符,举例说明如下:

SQL> select translate('123456789','456','abc') from dual;

TRANSLATE
---------
123abc789

SQL> select translate('123456789','456','ab') from dual;

TRANSLAT
--------
123ab789

SQL> select translate('123456789','4564','a') from dual;

TRANSLAT
-------
123a789

SQL> select translate('123456789','4564','abcd') from dual;

TRANSLATE
---------
123abc789

可以看到translate函数是以字符为单位逐一替换的,从第二、三个例子可以看出,如果第三个参数to_string的长度小于第二个参数from_string,那么from_string的后边超出的部分都被替换为空值。从第三、四个例子可见,如果某一个字符多次出现,则以第一次替换的内容为准。

以此可以实现的一个有用的功能是统计一个字符串中某个字符出现的次数:

select length(translate('expl','x'||'expl','x')) from dual

官方文档10g Release 2 (10.2) B14200-02 提供了一个例子如下:

Examples

The following statement translates a book title into a string that could be used (for example) as a filename. The from_string contains four characters: a space, asterisk, slash, and apostrophe (with an extra apostrophe as the escape character). The to_string contains only three underscores. This leaves the fourth character in the from_string without a corresponding replacement, so apostrophes are dropped from the returned value.

SELECT TRANSLATE('SQL*Plus User''s Guide', ' */''', '___') FROM DUAL;
--------------------
SQL_Plus_Users_Guide

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值