SQL 中的部分屏蔽 - Oracle

问题描述

给定的表有借记卡号码。我想写个代码把中间4个数字换成X的借记卡号进行屏蔽,表格如下:

enter image description here

我想要以下输出

enter image description here

我编写了以下代码,但出现错误

UPDATE
Debit_card_master
SET
Debit_card = LEFT(Debit_card,4) + REPLICATE(‘x’,4) + RIGHT(Debit_card,4) ;

另外,谁能帮我提供正确的 MysqL 代码

解决方法

您的代码看起来有点适合 MySQL(虽然不完全)。在 Oracle 中,您可以使用 regexp_replace():

regexp_replace(debit_card,'^(....)(....)','\1xxxx')

你可以先测试一下。在 update 中:

UPDATE Debit_card_master
    SET debit_card = regexp_replace(debit_card,'\1xxxx');

注意:这里假设数字总是 12 个字符。或者,更具体地说,它替换位置 5-8 中的字符。