简体   繁体   English

用于仅屏蔽值的第 1 个 5 个字符的 sql 查询

[英]sql query for masking only 1st 5 char of value

i need your help in creating the below query for "I have a column called 'A' with 10 digits in length I also have a column called 'B'我需要您的帮助来创建以下查询:“我有一个名为‘A’的列,长度为 10 位,我还有一个名为‘B’的列

   A                                   B
1234567890                           SAM
2345678912                           HELLO
7364557382                           MORNING 

I want to mask column A and replace the first 5 digits with column B.我想屏蔽 A 列并用 B 列替换前 5 位数字。

Post masking :后屏蔽:

SAM   67890  (with 2 spaces)
HELLO78912 
MORNI557382   

You can use string functions:您可以使用字符串函数:

select t.*, rpad(b, 5, ' ') || substr(a, 6) res
from mytable t

If a is a number, you can explictly cast it first:如果a是数字,您可以先显式转换它:

select t.*, rpad(b, 5, ' ') || substr(to_char(a), 6) res
from mytable t

You can use RPAD( B, 5, ' ' ) || SUBSTR( A, -5 )您可以使用RPAD( B, 5, ' ' ) || SUBSTR( A, -5 ) RPAD( B, 5, ' ' ) || SUBSTR( A, -5 ) to get the first 5 characters of B (right-padded with spaces if required) concatenated with the last 5 characters of A . RPAD( B, 5, ' ' ) || SUBSTR( A, -5 )获取B的前 5 个字符(如果需要,右填充空格)与A的最后 5 个字符连接。

For example:例如:

SELECT A,
       B,
       RPAD( B, 5, ' ' ) || SUBSTR( A, -5 ) AS masked_value
FROM   table_name

With the sample data:使用示例数据:

CREATE TABLE table_name ( A, B ) AS
SELECT 1234567890, 'SAM'     FROM DUAL UNION ALL
SELECT 2345678912, 'HELLO'   FROM DUAL UNION ALL
SELECT 7364557382, 'MORNING' FROM DUAL

Outputs:输出:

\n         A |一个 | B |乙 | MASKED_VALUE MASKED_VALUE\n---------: | ---------: | :------ | :------ | :----------- :-----------\n1234567890 | 1234567890 | SAM |山姆 | SAM 67890山姆 67890  \n2345678912 | 2345678912 | HELLO |你好 | HELLO78912你好78912  \n7364557382 | 7364557382 | MORNING |上午 | MORNI57382 MORNI57382  \n

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM