Solved

How to update table using a random date

Posted on 2008-10-24
4
1,142 Views
Last Modified: 2012-06-21
Hi!
I need to update a date column in a table with random dates. I've tried the script below, but it inserts the same date into all the fields. The DATEADD part on its own generates random dates just fine, and if you stick it in a loop it generates lots of different dates, but it doesn't work when run as part of an UPDATE statement.

Does anyone know how to fix this?

SQL Being used >>>>>>>>>>>>>>>>>

Update KT8
Set SubDate = DATEADD(second, rand()*36000, DATEADD(day, rand()*120, '2008-01-01 08:00:00'))


Thanks in advance!

Jason
0
Comment
Question by:jasonhick70
  • 2
4 Comments
 
LVL 9

Expert Comment

by:jamesgu
ID: 22796569
you may have to use NEWID() to set a random value for each row
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 125 total points
ID: 22796715
rand() will be unique per batch.  So if it's run for 1000 records, it will be the same for all.  Do you have a surrogate key (ID field) such as an identity?  You can use that in conjunction with your rand()*36000 to help make it a LITTLE more randonm.

something like :

Update KT8
Set SubDate = DATEADD(second, rand(id)*36000, DATEADD(day, rand()*120, '2008-01-01 08:00:00'))
0
 

Author Comment

by:jasonhick70
ID: 22797288
Hi! Thanks for your response guys.

The rand(id) does mix things up a little bit, but I was kinda looking for something MORE random! newid() sounds like it might do the job, but i'm not sure how to implement it.... just a newbie. Any pointers?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22797403
The problem is that rand() returns a decimal value between 0 and 1.  id only helps to give different values for each rand() per row but since you are adding only between 36000 and 0, you will only have a possible variance of 10 hours between your min and max date selected.  If you increase the 36000 to something much higher like 1 million (11 day span), or add that to minutes  and not seconds (25 day span) you will get more SEEMINGLY random data.
0

Featured Post

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.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

813 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