调试经验——使用Oracle REPLACE函数替换查询结果中的指定字符(Replace substring in Oracle)

需求:

查询出来的字段中带有一些前缀,用户不希望在报表中显示这些前缀。目前的操作方法是在Excel中手动替换掉。我在想,有没有办法自动化?使用VBA有点小题大做,能不能放到SQL中实现呢?可以,使用REPLACE函数即可。

代码:

REPLACE (UTABLE.UFIELD, 'NOT_NEEDED','')

顺便了解以下Oracle Database SQL Language Reference中对REPLACE函数的描述:

-----------------------------------------------------------------------------------------------------------------


Purpose


REPLACE returns char with every occurrence of search_string replaced with
replacement_string. If replacement_string is omitted or null, then all occurrences of
search_string are removed. If search_string is null, then char is returned.
Both search_string and replacement_string, as well as char, can be any of the data
types CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is in the
same character set as char. The function returns VARCHAR2 if the first argument is not a
LOB and returns CLOB if the first argument is a LOB.
REPLACE provides functionality related to that provided by the TRANSLATE function.
TRANSLATE provides single-character, one-to-one substitution. REPLACE lets you
substitute one string for another as well as to remove character strings.


Examples
The following example replaces occurrences of J with BL:
SELECT REPLACE('JACK and JUE','J','BL') "Changes"
FROM DUAL;
Changes
--------------
BLACK and BLUE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用`INSTR`函数来查找包含多个字符的子字符串。该函数的语法如下: ``` INSTR(string, substring1 [, start_position [, occurrence [, substring2 ]]]) ``` 其,`string`参数是要查找的字符串,`substring1`参数是要查找的子字符串,`start_position`参数是可选的,表示从哪个位置开始查找,默认是1,`occurrence`参数也是可选的,表示要查找的子字符串出现的次数,默认是1,`substring2`参数也是可选的,表示要替换的子字符串。 例如,要查找包含多个字符的子字符串,可以使用以下语句: ``` SELECT INSTR('Hello world, how are you today?', 'world') FROM dual; ``` 该语句将返回子字符串"world"在原始字符的位置,即7。 要截取字符的一部分,可以使用`SUBSTR`函数。该函数的语法如下: ``` SUBSTR(string, start [, length]) ``` 其,`string`参数是要截取的字符串,`start`参数是截取的起始位置,`length`参数是可选的,表示要截取的长度,默认是从起始位置截取到字符串末尾。 例如,要截取字符的一部分,可以使用以下语句: ``` SELECT SUBSTR('Hello world, how are you today?', 7, 5) FROM dual; ``` 该语句将返回从原始字符串的第7个字符开始的5个字符,即"world"。 要替换字符的子字符串,可以使用`REPLACE`函数。该函数的语法如下: ``` REPLACE(string, substring1 [, substring2]) ``` 其,`string`参数是要替换字符串,`substring1`参数是要被替换的子字符串,`substring2`参数是替换后的子字符串。 例如,要替换字符的子字符串,可以使用以下语句: ``` SELECT REPLACE('Hello world, how are you today?', 'world', 'everyone') FROM dual; ``` 该语句将返回将字符的"world"替换为"everyone"后的结果,即"Hello everyone, how are you today?"。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值