Solved

Insert Random Date within Range into MySQL

Posted on 2009-05-13
8
785 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
  • 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now