出发日期大于抵达日期

时间:2013-02-19 16:32:12

标签: tsql

如何确保我的出发日期大于以下代码中的到达日期。

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

由于

韦恩

2 个答案:

答案 0 :(得分:2)

DATEADD采用整数...并且随机化返回的任何十进制值都将被截断。因此,您可能只需将0添加到ArrivalDate,从而导致两个日期相同。

您可以通过在随机化中添加至少1来解决此问题:

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

答案 1 :(得分:1)

尝试此查询

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