开发者

PostgreSQL: change date by the random number of days

开发者 https://www.devze.com 2022-12-17 09:03 出处:网络
How can I change the date by the random number of days in PostgreSQL? Unfortunately Generate a random number in the range 1 - 10

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

Unfortunately Generate a random number in the range 1 - 10 solution with trunc doesn't work:

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

results in:

ERROR:  operator 开发者_JS百科does not exist: date + double precision
LÍNEA 1: select date(now()) + (trunc(random()  * 20)) 


select date(now() + trunc(random()  * 20) * '1 day'::interval);

Any existing date column can also be used in place of now()

See: https://www.postgresql.org/docs/current/functions-datetime.html


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号