Solved

Insert Random Date within Range into MySQL

Posted on 2009-05-13
8
790 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cassandra Select Query 1 55
Oracle Pivot Question 8 45
SQL Help - SELECT Statement 6 39
Oracle 10g - Select rows into colums 4 12
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

920 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

12 Experts available now in Live!

Get 1:1 Help Now