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

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

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.



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

James K
Flowing Fount of Yak Knowledge

3865 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:
	DepartureDate = DATEADD( day, 1 + (LengthofStay-1)*RAND(checksum(NEWID())),ArrivalDate)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.77 seconds. Powered By: Snitz Forums 2000