Re: Using random() in update produces same random value for all

From: Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com>
To: Alex Magnum <magnum11200(at)gmail(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Using random() in update produces same random value for all
Date: 2018-01-14 10:24:04
Message-ID: CAD3a31Wkji3HCPxcRDdA6P6xHjjURV-YP_Z6wYitqSpc2G0e4g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Jan 14, 2018 at 2:01 AM, Alex Magnum <magnum11200(at)gmail(dot)com> wrote:

> Hi,
> i am trying to update a table with some random dates but that does not
> seem to work.
>
> UPDATE table
> SET last_update=now()::date-(SELECT (random() * 5)::INTEGER + 1)
>
> The updated field is always set to the same. Is there a way to make it
> random for every record?
>
> I could run it through a function but I wonder if there is s simpler way.
>
>
I verified this with a SELECT, not an UPDATE, but I think leaving this as a
simple expression will do what you want. Just leave out the SELECT:

UPDATE table
SET last_update=now()::date-((random() * 5)::INTEGER + 1)

Cheers,
Ken

--
AGENCY Software
A Free Software data system
By and for non-profits
*http://agency-software.org/ <http://agency-software.org/>*
*https://demo.agency-software.org/client
<https://demo.agency-software.org/client>*
ken(dot)tanzer(at)agency-software(dot)org
(253) 245-3801

Subscribe to the mailing list
<agency-general-request(at)lists(dot)sourceforge(dot)net?body=subscribe> to
learn more about AGENCY or
follow the discussion.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Peter J. Holzer 2018-01-14 10:27:58 Re: Sv: Re: Sv: Re: Sv: Re: Sv: Re: data-checksums
Previous Message Alex Magnum 2018-01-14 10:01:11 Using random() in update produces same random value for all