Mysql insert random datetime in a given datetime range

Found this nice sql.

I needed to randomize a date for the random data I was using to test a batch job I was fixing.  I would have used several excellent random data generators available online except my internet sucks so have to do this on my own.

This works perfectly even for leap years:select from_unixtime( unix_timestamp(‘2000-1-1’) + floor( rand() * ( unix_timestamp(‘2010-12-31’) – unix_timestamp(‘2000-1-1’) + 1 ) ))The idea is simple: Just take a random timestamp between the two timestamps, then convert it to a datetime using from_unixtime. This way you can ensure that each option has equal probability.

Source: sql – Mysql insert random datetime in a given datetime range – Stack Overflow