MySQL Generate Random Date


For testing, it can be nice to generate random date fields in sql. Because this version uses UNIX_TIMESTAMP it is limited to the range 1970 to 2038.
-- This will generate a random date between now and 2003
SELECT FROM_UNIXTIME(RAND() * (UNIX_TIMESTAMP() - UNIX_TIMESTAMP('2003-01-01')) + UNIX_TIMESTAMP('2003-01-01'));
 
UPDATE mytable 
  SET datefield = 
    FROM_UNIXTIME(RAND() * (UNIX_TIMESTAMP() - UNIX_TIMESTAMP('2003-01-01')) + UNIX_TIMESTAMP('2003-01-01'))
  WHERE 1
  AND somevalue=someothervalue;
code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)