oracle 掩码字段(贷方编号)

vyu0f0g1  于 2023-03-22  发布在  Oracle
关注(0)|答案(3)|浏览(133)

我有一个 16位 * 的信用卡号码序列,我试图覆盖该序列的 * 前4 和 * 后6* 号

CASE(target_number WHEN "C~"
    THEN(LEFT(target_number, 4) +
         IF(LEN(TRIM(target_number)) > 4, "*******~", "~") +
         CASE(LEN(TRIM(target_number)) WHEN IS > 15 THEN
              RIGHT(TRIM(target_number), 6) WHEN IS = 15 THEN
              RIGHT(TRIM(target_number), 5) WHEN IS = 14 THEN
              RIGHT(TRIM(target_number), 4) ELSE "~")) ELSE
     target_number)
s2j5cfk0

s2j5cfk01#

我有一个16位信用卡号码的序列,我试图覆盖该序列的前4位和最后6位号码
如果是这样的话,我会说,有比你更简单的方法。其中一个是

SQL> with test (ccard) as
  2    (select '0123456789123456' from dual)
  3  select '****' || substr(ccard, 5, 6) || '******' result
  4  from test;

RESULT
------------------------------
****456789******

SQL>
p5fdfcr1

p5fdfcr12#

您可以创建一个视图并屏蔽其中的数据,并为希望查看屏蔽数据的用户给予对该视图的访问权限。您应该调整regexp_replace以满足您的需要。
这就是我如何实现它。

CREATE TABLE CARDS_TBL (
  CARD_ID NUMBER
    GENERATED BY DEFAULT AS IDENTITY,
  CARD_STR VARCHAR2(16) NOT NULL,
  PRIMARY KEY (CARD_ID)
);

INSERT INTO CARDS_TBL(CARD_STR) VALUES('4024007187788590');
INSERT INTO CARDS_TBL(CARD_STR) VALUES('5432223398564536');
INSERT INTO CARDS_TBL(CARD_STR) VALUES('5430445512530934');
INSERT INTO CARDS_TBL(CARD_STR) VALUES('4020156755227854');
INSERT INTO CARDS_TBL(CARD_STR) VALUES('5431248766892318');

CREATE OR REPLACE VIEW CARDS AS
  SELECT
    CARD_ID,
    REGEXP_REPLACE(CARD_STR, '(^\d{3})(.*)(\d{4}$)', '\1**********\3') AS CARD_STR
  FROM CARDS_TBL;

CREATE OR REPLACE TRIGGER CARDS_TBL_TRG_INSERT
  INSTEAD OF INSERT ON CARDS 
  FOR EACH ROW
BEGIN
  INSERT INTO CARDS_TBL (CARD_STR) VALUES (:NEW.CARD_STR);
END;
/

INSERT INTO CARDS_TBL (CARD_STR) VALUES ('2222333344445555');

SELECT * FROM CARDS_TBL;

UPDATE CARDS_TBL 
  SET CARD_STR = '2222333344445566'
  WHERE CARD_ID = 6;
/

SELECT * FROM CARDS;

CARD_ID CARD_STR
1   402**********8590
2   543**********4536
3   543**********0934
4   402**********7854
5   543**********2318
6   222**********5566

SELECT * FROM CARDS_TBL;

CARD_ID CARD_STR
1   4024007187788590
2   5432223398564536
3   5430445512530934
4   4020156755227854
5   5431248766892318
6   2222333344445566
ercv8c1e

ercv8c1e3#

您可以动态地重新生成星号,如下面的示例中所示:

WITH tt AS
(  
  SELECT CASE WHEN level <= 4 OR level > LENGTH(cc_number) - 6 THEN 
                   '*'
              ELSE 
                   SUBSTR(cc_number,level,1)
               END AS chr 
    FROM t
 CONNECT BY level <= LENGTH(cc_number)
)
SELECT LISTAGG(chr) AS masked_cc_number
  FROM tt

其中条件中的46可能会根据星号的数量而被替换
Demo

相关问题