Solved

How to update table using a random date

Posted on 2008-10-24
4
1,144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

736 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