表字段脱敏导出(随机字符串、随机数)

## 26个大小写字母,随机生成字符串
create or replace function random_string(integer)

returns text as

$body$

         select array_to_string(array(select substring('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' FROM (ceil(random()*62))::int FOR 1) FROM generate_series(1, $1)), '');

$body$

language sql volatile; 

测试:
select random_string(10);


##生成9位随机数字
select  (random()*(10^9))::integer

##生成84 +  9位随机数
select  '84' || (random()*(10^9))::integer


#转换脱敏字段,并根据原有表字段类型转换字段类型

create table tableName_temp as (

select id,
(case when coalesce(name_full,'') = '' then null else random_string(10)::varchar end) as name_full,
date_birth,
(case when coalesce(ident_card,'') = '' then null else (random()*(10^9))::varchar end) as ident_card,
date_applied,
(case when coalesce(mobile_phone,'') = '' then null else ('84' || (random()*(10^9))::integer)::varchar end) as mobile_phone,
count_paid,cnt_days_past_due,
(case when coalesce(home_phone,'') = '' then null else ('84' || (random()*(10^9))::integer)::varchar end) as home_phone,
(case when coalesce(work_phone,'') = '' then null else ('84' || (random()*(10^9))::integer)::varchar end) as work_phone,
(case when coalesce(phone1,'') = '' then null else ('84' || (random()*(10^9))::integer)::varchar end) as phone1,
(case when coalesce(phone2,'') = '' then null else ('84' || (random()*(10^9))::integer)::varchar end) as phone2,
(case when coalesce(phone3,'') = '' then null else ('84' || (random()*(10^9))::integer)::varchar end) as phone3,
status
 from tableName );


#将public.black_data导入public.tableName_temp
insert into public.black_data_temp
(id,name_full,date_birth,ident_card,date_applied,mobile_phone,count_paid,cnt_days_past_due,home_phone,
work_phone,phone1,phone2,phone3,status) 
select id,name_full,date_birth,ident_card::integer,date_applied,mobile_phone,count_paid,cnt_days_past_due,home_phone,
work_phone,phone1,phone2,phone3,status from public.tableName;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值