【问题标题】:We need to mask data for the String up to fixed length in Oracle我们需要在 Oracle 中将字符串的数据屏蔽到固定长度
【发布时间】:2022-01-09 19:06:31
【问题描述】:

我正在尝试屏蔽以下字符串的数据:

这是新的 ADHAR NUMBER 123456789989 这是从客户名称(如 345678)到字符串的字符串 3456798983。

在上面的数据中,我想屏蔽从ADHAR NUMBER 开始的数据,长度不超过60 个字符。

输出:

这是新的 ******************************************* **************Customer Name like 345678 to a String .

谁能帮忙

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    一点点 substr + instr 就可以完成这项工作(前两行中的样本数据;查询从第 3 行开始):

    SQL> with test (col) as
      2    (select 'This is the new ADHAR NUMBER 123456789989 this is the string 3456798983 from Customer Name like 345678 to a String .' from dual)
      3  select substr(col, 1, instr(col, 'ADHAR NUMBER') - 1) ||
      4         lpad('*', 60, '*') ||
      5         substr(col, instr(col, 'ADHAR NUMBER') + 60) result
      6  from test;
    
    RESULT
    --------------------------------------------------------------------------------
    This is the new ************************************************************ Cus
    tomer Name like 345678 to a String .
    
    
    SQL>
    

    【讨论】:

    • 如果字符串接近最大长度并且子字符串在字符串末尾附近匹配db<>fiddle,这可能会失败
    • @MTO - 你能在 oracle 中提供任何解决方案吗
    • @EX08,你至少尝试我建议的代码了吗?
    • @EX08 如果输入字符串很短,这将起作用。如果您使用的是VARCHAR2(4000) 字符串,那么您需要考虑超出最大字符串长度并处理这种情况。
    • 不要介意最大字符串长度(Oracle 限制)。此解决方案将始终附加 60 个星号,即使初始字符串只有 55 个字符长。目前尚不清楚这是否适用于 OP,但在标题中他明确表示“最多”固定长度。 ****** 之类的东西通常不被视为三字符字符串的掩码。
    【解决方案2】:

    这是一个涵盖所有可能性的解决方案(我认为)。请注意 WITH 子句中的不同输入(这不是解决方案的一部分 - 删除它,并在查询中使用您的实际表和列名称)。这就是人们应该如何测试他们的解决方案 - 考虑所有可能的情况,包括NULL 输入、非NULL 输入字符串,它不包含“魔术词”,字符串开头有“魔术词”等。

    解决方案没有解决一种重要情况,即确切的子字符串 'ADHAR NUMBER' 不是两个完整的单词,而是较长单词的一部分 - 例如 'BHADHAR NUMBERS'。在这种情况下,输出将类似于 'BH****************' 屏蔽 ADHAR NUMBER 和 NUMBER 后面的 S 以及更多字符,总共最多 60 个。

    请注意,输出字符串与输入的长度相同。这通常是“掩蔽”定义的一部分。

    with
      test (col) as (
        select 'This is the new ADHAR NUMBER 123456789989 this is the string ' ||
                '3456798983 from Customer Name like 345678 to a String.'
                                                              from dual union all
        select 'This string does not contain the magic words' from dual union all
        select 'ADHAR NUMBER 12345'                           from dual union all
        select 'Blah blah ADHAR NUMBER 1234'                  from dual union all
        select null                                           from dual union all
        select 'Another blah ADHAR NUMBER'                    from dual
      )
    select case when pos > 0
                then
                  substr(col, 1, pos - 1) ||
                  rpad('*', least(60, length(col) - pos + 1), '*') ||
                  substr(col, pos + 60)
                else col end as masked
    from   (
             select col, instr(col, 'ADHAR NUMBER') as pos
             from   test
           )
    ;
    
    MASKED                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This is the new ************************************************************ Customer Name like 345678 to a String.
    This string does not contain the magic words
    ******************
    Blah blah *****************
    
    Another blah ************
    

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2015-06-25
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      相关资源
      最近更新 更多