Mysql insert random datetime in a given datetime range
Insert a random datetime within a preset range using TIMESTAMPADD
and RAND()
. Generate a random number of seconds and add it to the start date. Here’s a concise query to insert a random datetime between two dates in a table:
Remember to plug in your table_name
, datetime_column
, and your required date range.
Achieving true randomness
Ensure that each moment within the range has an equal probability of being selected. Use FROM_UNIXTIME
and UNIX_TIMESTAMP
👈 it's not a band name, it's to convert datetimes to and from Unix epoch (seconds since 1970), thus avoiding any bias:
Cross-check and validate if these random dates are leap year proof and adjust the range to your needs.
Constraints within anomalies
Keep your random dates within a specific business-relevant range, by adding an extra layer of validation:
This subquery with a WHERE clause 🕶️ handles any datetimes that may fall outside the range due to rounding or other anomalies.
Adopting distribution strategies
When you need to control the standard deviation or when the distribution of random datetimes has to match a pattern (say, more Mondays), tweak how you generate randomness:
Customise the case statement to orient your randomness towards desired dates or times.
Optimizing performance leaps
Got large datasets or need random datetime inserts—like a lot? Performance takes a stride. Use variable assignments to lighten the compute during the insert operation:
Pre-calculating parts of the statement outside the INSERT
command is like eating your breakfast before running the marathon.
Was this article helpful?