需求描述
- 姓名
- 中文姓名
- 2个字码最后一个字
- 大于等于3个字,只留第一个和最后一个字
- 英文姓名
- 保留第一个和最后一个字符 例如 Jack.Antg 脱敏后变成J***.***g
- 中文姓名
- 手机号
- 11位 则保留前3位和最后4位
- 其他位数的脱敏中部三分之一
- 证件号
- 大于8位,除了前后4位都脱敏
- 小于等于8大于4 ,除了后4位都脱敏
- 小于等于4,不脱敏
实现
姓名脱敏
case when length(name)<1 then null
when length(name)=1 then name
when length(name)=2 then concat(substr(name,1,1),'*')
when length(name)>=3 then concat(substr(name,1,1),substr(regexp_replace(name,'[^.]','*'),2,length(name)-2),substr(name,-1))
end as name
证件号脱敏
case when length(id_no)<=4 then id_no
when length(id_no) in(5,6,7,8) then concat(substr(regexp_replace(id_no,'.','*'),1,length(id_no)-4),substr(id_no,-4))
when length(id_no)>8 then concat(substr(id_no,1,4),substr(regexp_replace(id_no,'.','*'),2,length(id_no)-8),substr(id_no,-4))
end as id_no
手机号脱敏
case
when length(phone)<1 then null
when length(phone)=2 then concat(substr(phone,1,1),'*')
when length(phone) in(3,4,5,6,7,8,9,10) then concat(substr(phone,1,cast(round(length(phone)/3.0) as int)),substr(regexp_replace(phone,'.','*'),cast(round(length(phone)/3.0) as int)+1,cast(round(length(phone)/3.0) as int)),substr(phone,-(length(phone)-2*cast(round(length(phone)/3.0) as int))))
when length(phone)=11 then concat(substr(phone,1,3),'****',substr(phone,-4))
when length(phone)>=12 then concat(substr(phone,1,cast(round(length(phone)/3.0) as int)),substr(regexp_replace(phone,'.','*'),cast(round(length(phone)/3.0) as int)+1,cast(round(length(phone)/3.0) as int)),substr(phone,-(length(phone)-2*cast(round(length(phone)/3.0) as int))))
end as phone