如何在PostgreSQL中屏蔽电子邮件地址字符串?
我想如下屏蔽电子邮件 将testemail@gmail.com转换为te ***** il@gmail.com
我尝试使用来解决
POSITION
来标识@符号LENGTH
个POSITION
左边的字符但是PostgreSQL没有类似于excel =REPLACE (old_text, start_num, num_chars, new_text)
的简洁解决方案,您可以在其中设置start_num
和num_chars
答案 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)$$;