views:

54

answers:

2

How can I change the date by the random number of days in PostgreSQL?

Unfortunately http://stackoverflow.com/questions/1400505/postgresql-random-number-range-1-10 solution with trunc doesn't work:

select date(now()) + (trunc(random()  * 20)) 

results in:

ERROR:  operator does not exist: date + double precision
LÍNEA 1: select date(now()) + (trunc(random()  * 20)) 
+1  A: 
select date(now() + trunc(random()  * 20) * '1 day'::interval);
Milen A. Radev
Analfire syntax. But thanks anyway.
Bogdan Gusiev
+1  A: 

How random? For example - if you want random data from last year:

select cast( now() - '1 year'::interval * random()  as date );
depesz