Skip to content
Advertisement

Generate random date into series [closed]

I use this SQL query to generate random data:

INSERT into tasks SELECT generate_series(1,3) AS id, 
md5(random()::text) AS business_name, 
select NOW() + (random() * (NOW()+'90 days' - NOW())) + '30 days' AS created_at,
md5(random()::text) AS meta_title,
md5(random()::text) AS status,
md5(random()::text) AS title,
md5(random()::text) AS type

I get this error:

ERROR:  syntax error at or near "select"
LINE 3: select NOW() + (random() * (NOW()+'90 days' - NOW())) + '30 ...
        ^
SQL state: 42601
Character: 105

What is the proper way to generate random date?

Advertisement

Answer

Fixed the created_at expression

INSERT into tasks
SELECT generate_series(1,3) AS id, 
md5(random()::text) AS business_name, 
now() + random() * interval 'P90DT1H' + '30 days' AS created_at,
md5(random()::text) AS meta_title,
md5(random()::text) AS status,
md5(random()::text) AS title,
md5(random()::text) AS type;

(NOW() + '90 days' - NOW()) is equivalent to interval 'P90DT1H' because of the summer time one hour shift.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement