Random character insert - Postgresql

Asked

Viewed 96 times

1

I have a Function that makes an Insert when a certain action occurs, I’m trying to insert together random letters but I’m not finding anything related to this, It is possible to do this in Postgresql?

  • https://stackoverflow.com/a/3972983/4713574

  • @Rovannlinhalis but in this example was select I can use it in an Insert?

  • yes, just use the same function

  • Thank you very much indeed @Rovannlinhalis

1 answer

0

You will have to create a function see the code below:

create or replace function shuffle(text)
returns text language sql as $$
    select string_agg(ch, '')
    from (
        select substr($1, i, 1) ch
        from generate_series(1, length($1)) i
        order by random()
        ) s
        $$;


insert into pessoas ( nome, criado, obs, sobrenome)values('nome', '2018-01-16 10:53:04.033', '{s, o, p, t}', (select left(shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'), 1)));

The way out will be:

[QUERY    ] create or replace function shuffle(text)
        returns text language sql as $$
            select string_agg(ch, '')
            from (
                select substr($1, i, 1) ch
                from generate_series(1, length($1)) i
                order by random()
                ) s
                $$
[QUERY    ] insert into pessoas ( nome, criado, obs, sobrenome)values('nome', '2018-01-16 10:53:04.033', '{s, o, p, t}', (select left(shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'), 1)))

If you have difficulty post your function;

I hope I’ve helped;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.