• wafw1971 (2/20/2013)


    The following query gives me a random date between 1 and 28 days after the arrival date:

    SELECT ArrivalDate, DATEADD(day, 1 + RAND(checksum(NEWID()))

    * LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate

    FROM Bookings, LengthOfStay

    However when I use the update query below it only gives me between 1 and 2 days after the arrival date

    USE Occupancy

    Update B

    Set DepartureDate = DATEADD(day, 1 + RAND(checksum(NEWID()))*1.5 * L.LengthofStay, B.ArrivalDate)

    FROM LengthOfStay L, Bookings B

    Does anyone know why, and if so how do I change it?

    Thanks

    Wayne

    If you run this multiple times:

    SELECT 1 + RAND(checksum(NEWID()))

    You should see that the number returned is always between 1 and 2.

    If you want it to return a number between 1 and 28, use this:

    SELECT 1 + ABS(checksum(NEWID())) % 28


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St