Solved

How to update table using a random date

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

9 Experts available now in Live!

Get 1:1 Help Now