如何在PostgreSQL中屏蔽电子邮件

时间:2019-12-19 10:41:50

标签: postgresql

如何在PostgreSQL中屏蔽电子邮件地址字符串?

我想如下屏蔽电子邮件 将testemail@gmail.com转换为te ***** il@gmail.com

  • 取消屏蔽电子邮件域之前的前两个字符和后两个字符(@ 符号)
  • 使用*
  • 掩盖所有字符

我尝试使用来解决

  • POSITION来标识@符号
  • LENGTHPOSITION左边的字符

但是PostgreSQL没有类似于excel =REPLACE (old_text, start_num, num_chars, new_text)的简洁解决方案,您可以在其中设置start_numnum_chars

1 个答案:

答案 0 :(得分:0)

尝试一下:

SELECT overlay(
          'testemail@gmail.com'
           placing repeat('*',
                          position('@' in 'testemail@gmail.com') - 5
                         )
           from 3
           for position('@' in 'testemail@gmail.com') - 5
       );

       overlay       
---------------------
 te*****il@gmail.com
(1 row)

为了方便起见,您可以在其中创建一个SQL函数:

CREATE FUNCTION mask_email(text) RETURNS text
   LANGUAGE SQL IMMUTABLE AS
$$SELECT overlay($1 placing repeat('*', position('@' in $1) - 5) from 3 for position('@' in $1) - 5)$$;