Solved

How to update table using a random date

Posted on 2008-10-24
4
1,143 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS - Date Report Options 2 29
Syntax error with Dates where clause 10 49
SQL group by query to return records with highest value 6 25
Acces SQL Insert Problem 6 32
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

830 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