[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Insert Random Date within Range into MySQL

Posted on 2009-05-13
8
Medium Priority
?
850 Views
Last Modified: 2012-05-06
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
Comment
Question by:chrismdoyle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 24375688
Try this:
select curdate() - interval rand()*365*2 day

Open in new window

0
 
LVL 14

Expert Comment

by:racek
ID: 24375746
SELECT DATE_SUB(NOW() ,INTERVAL FLOOR(1 + (RAND() * 200), day)
0
 
LVL 14

Expert Comment

by:racek
ID: 24375874
sorry
SELECT curdate() -  FLOOR(1 +  (RAND() * 730)) ;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chrismdoyle
ID: 24375880
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
 
LVL 14

Expert Comment

by:racek
ID: 24375937
this is for 2 years back
SELECT curdate() -  FLOOR(1 +  (RAND() * 730)) ;
100 days ahead
SELECT curdate() +  FLOOR(1 +  (RAND() * 100)) ;
0
 

Author Comment

by:chrismdoyle
ID: 24376104
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
 

Author Comment

by:chrismdoyle
ID: 24376167
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
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24377190
>> 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question