learning_notes

学习笔记

View project on GitHub

测试数据

  1. 生成身份证
create or replace function gen_id_card(  
--   a date,  
--   b date  
)   
returns text as $$  
select lpad((random()*99)::int::text, 2, '0') ||   
       lpad((random()*99)::int::text, 2, '0') ||   
       lpad((random()*99)::int::text, 2, '0') ||   
       to_char(to_timestamp(random()*1000000000),'yyyymmdd') ||   
--        to_char(a + (random()*(b-a))::int, 'yyyymmdd') ||   
       lpad((random()*99)::int::text, 2, '0') ||   
       random()::int ||   
       (case when random()*10 >9 then 'X' else (random()*9)::int::text end ) ;  
$$ language sql strict;  
  1. 生成汉字
create or replace function gen_hanzi(int) returns text as 
$$
                 
declare        
 res text;        
begin        
 if $1 >=1 then        
   select string_agg(chr(19968+(random()*20901)::int), '') into res from generate_series(1,$1);        
   return res;        
 end if;        
 return null;        
end;        

$$
language plpgsql strict;  
  1. 生成jsonb数据
create or replace function gen_fids(int) returns text as 
$$
                 
declare        
 res text; 
begin        
 if $1 >=1 then   
   select string_agg(floor(random()*100))::text, ',') into res from generate_series(1,$1); 
   return '{"fids":[' || res || ']}';        
 end if;        
 return null;        
end;        

$$
language plpgsql strict;