如何通过从当前 date_time 中减去 date_time 字段来计算天数?

How to calculate number of days by subtracting date_time field from current date_time?

I have reservations table 用来存储酒店预订,这个 table 有 reservation_id, customer_id, rommno, checking date 还有另一个可选字段,c​​heckout人员结帐时将使用的字段,状态字段也将更新为结帐状态,首先放置检查状态。

我需要一个查询来查找从检查日期起的停留日期数。 所需的结果将如下所示:

customer_id, roomno, checkin_date, due_nights

到期夜将是减去 checkin_date 和当前日期的结果,我尝试了这个 sql 查询并且它 returns #Error

SELECT DATEDIFF(reservations.due_nights,  now(), reservations.checkin_date) from reservations

我的 table 视图低于 link

current table view click

你的查询有误

SQL 服务器 datediff 的语法是

DATEDIFF(datepart,startdate,enddate)

另外 sql 服务器获取当前日期的函数是 getdate() 而不是 now()

所以在你的情况下它将是

Select DATEDIFF(DAY,reservations.checkin_date, getdate())

例如:-

select DATEDIFF(Day,'06-07-2015 14:00:00',GETDATE())
     will return 10

MS-ACCESS

DateDiff ( interval, date1, date2, [firstdayofweek], [firstweekofyear])

 DATEDIFF("d",reservations.checkin_date,Now())

  where d represents the interval as day

eg: -  DateDiff ("d", #15/10/2003#, #22/11/2003#)

       result will be 38