在Oracle中用'*'替换字符串中未知数量的字符

时间:2018-12-14 10:32:14

标签: sql oracle

给出一列 MEMO_TXT 在一个 MEMO 表中具有以下文本:
'Password changed from: 12345 to: abcdefg.'

我如何替换12345abcdefg中的 上面的文本使用大小相等的'*'个字符?

like,
    'Password changed from: 12345 to: abcdefg.'
'Password changed from: ***** to: *******.'

上述字符串中的部分可能会有所不同,并且由于无法确定固定的模式,因此我不确定如何使用REPLACE编写UPDATE查询

3 个答案:

答案 0 :(得分:1)

尝试使用REGEXP_REPLACE函数。

答案 1 :(得分:1)

如果消息文本始终采用这种格式,则旧的SUBSTR + INSTR + REPLACE可以胜任。

SQL> WITH test (col)
  2       AS (SELECT '&message' FROM DUAL),
  3       pos
  4       AS (SELECT col,
  5                  INSTR (col, ':', 1, 1)
  6                     colon_1,
  7                  INSTR (col, ':', 1, 2)
  8                     colon_2,
  9                  LENGTH (col) len
 10             FROM test),
 11       inter
 12       AS (SELECT col,
 13                  trim(SUBSTR (col, colon_1 + 2, colon_2 - 4 - colon_1)) old_pw,
 14                  TRIM (RTRIM (SUBSTR (col, colon_2 + 1, len - colon_2), '.'))
 15                     new_pw
 16             FROM pos)
 17  SELECT REPLACE (REPLACE (col, old_pw, LPAD ('*', LENGTH (old_pw), '*')),
 18                  new_pw,
 19                  LPAD ('*', LENGTH (new_pw), '*')) result
 20    FROM inter;
Enter value for message: Password changed from: 12345  to:  abcdefg.

RESULT
-------------------------------------------
Password changed from: *****  to:  *******.

SQL> /
Enter value for message: Password changed from: abxyz44#  to:  87ZU_3.

RESULT
----------------------------------------------
Password changed from: ********  to:  *******

SQL>

答案 2 :(得分:-1)

select rpad('*', 10, '*')

我认为此链接可以为您提供帮助:

https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions159.htm#SQLRF06103