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

需求:

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

代码:

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

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

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

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

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

你可能感兴趣的:(数据库(DB))