Advertisement
roganhamby

Random Strings

Apr 27th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create or replace function rogan.random_string(length integer) returns text as
  2. $$
  3. declare
  4.   chars text[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
  5.   result text := '';
  6.   i integer := 0;
  7. begin
  8.   if length < 0 then
  9.     raise exception 'Given length cannot be less than 0';
  10.   end if;
  11.   for i in 1..length loop
  12.     result := result || chars[1+random()*(array_length(chars, 1)-1)];
  13.   end loop;
  14.   return result;
  15. end;
  16. $$ language plpgsql;
  17.  
  18. select rogan.random_string(14);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement