Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Randomise Date
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

wafw1971
Yak Posting Veteran

75 Posts

Posted - 02/19/2013 :  09:21:40  Show Profile  Reply with Quote
I have a Arrival date of 01/01/2012 the Departure date can be any date between 02/01/2012 and 29/01/2012 and needs to added to the database either via an Update/Insert Statement. Can you help with the code.

SELECT ArrivalDate, DATEADD(day, RAND(checksum(NEWID()))
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate
FROM Bookings, LengthOfStay
ORDER BY ArrivalDate

But the Departure date cannot be the same as the arrival date.

Thanks

Wayne

Edited by - wafw1971 on 02/19/2013 11:08:39

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 02/19/2013 :  11:49:43  Show Profile  Reply with Quote
Are you trying to randomize the date of departure with the constraint that it be between January 3 and January 29? Or, are you asking how to insert it into another table? If you are trying to randomize:
DATEADD( day, 1 + 26*RAND(checksum(NEWID())),'1/2/2012')
If you are trying to update a column, the code would be something like this:
UPDATE tbl SET 
	DepartureDate = DATEADD( day, 1 + (LengthofStay-1)*RAND(checksum(NEWID())),ArrivalDate)
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000