Solved

Insert Random Date within Range into MySQL

Posted on 2009-05-13
8
814 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 500 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

696 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