• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 865
  • Last Modified:

Insert Random Date within Range into MySQL

Title is pretty self explanatory. I just want to use MySQL command line only to insert a random date into rows. The catch is the date needs to be random, but also within the range of the Last 2 years. Any help would be much appreciated.
0
chrismdoyle
Asked:
chrismdoyle
  • 3
  • 3
  • 2
1 Solution
 
Roger BaklundCommented:
Try this:
select curdate() - interval rand()*365*2 day

Open in new window

0
 
racekCommented:
SELECT DATE_SUB(NOW() ,INTERVAL FLOOR(1 + (RAND() * 200), day)
0
 
racekCommented:
sorry
SELECT curdate() -  FLOOR(1 +  (RAND() * 730)) ;
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
chrismdoyleAuthor Commented:
I'll try these now, additionally, if I wanted to make sure that the date range was within 1-2 years, but nothing newer than 1 year?
0
 
racekCommented:
this is for 2 years back
SELECT curdate() -  FLOOR(1 +  (RAND() * 730)) ;
100 days ahead
SELECT curdate() +  FLOOR(1 +  (RAND() * 100)) ;
0
 
chrismdoyleAuthor Commented:
CXR's implementation is by far the best. It produces output like 2009-03-30. Instead of a string of #'s. CXR, do you know how to get a random time to appear with that as well?
0
 
chrismdoyleAuthor Commented:
Ok, here's the way to do it to have a random day and time:


select NOW() - interval rand()*365*2 day

Open in new window

0
 
Roger BaklundCommented:
>> if I wanted to make sure that the date range was within 1-2 years, but nothing newer than 1 year?

Subtract 1 year and then subtract a random number of days between 0 and 365:
select curdate() - interval 1 year - interval rand()*365 day;

Open in new window

0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now