Last Updated: February 25, 2016
· bjacog

Random epoch time in SQL

We save all dates as bigint for a couple of reason that are off topic.

In order to test some reporting I wanted to fill a date field with random dates between a specific start and end date.

update mytable set some_date_field =  1372629600 + round(random() * 2592000) + 1

In the above query, the first number 1372629600 is my first possible date.
The second number, 2592000, is the difference between my first date and last date.
This effectively gives me random dates between the first day of the month and last day ofthe month.