plpgsql function to generate random readable strings
Posted
by Peter
on Stack Overflow
See other posts from Stack Overflow
or by Peter
Published on 2010-03-26T04:31:23Z
Indexed on
2010/03/26
4:33 UTC
Read the original article
Hit count: 561
plpgsql
|postgresql
Hi
I have written the following function but it's isn't returning anything when I run it. Can somebody help identify the issue?
CREATE OR REPLACE FUNCTION GenerateReadableRandomString (
len INT
) RETURNS varchar AS $$ DECLARE validchars VARCHAR; randomstr VARCHAR; randint INT; i INT; BEGIN validchars := 'ABCEFHJKLMNPRTWXY3478'; i := 0;
LOOP
randint := ceil(random() * char_length(validchars));
randomstr := randomstr || substring(validchars from randint for 1);
i := i + 1;
EXIT WHEN i = len;
END LOOP;
RETURN randomstr;
END; $$ LANGUAGE plpgsql;
© Stack Overflow or respective owner