问题
How can I subtract two times in an Access table?
query must do it automatically. I need to subtract left time with arrived, so then I get hours of working employed:
Query must fill hours column in table, so I can get it in c# program.
[Left]- [Arrived]

回答1:
In access, if the fields are date/time type, it is as simple as substract them and format output as desired. The substraction results in days and fraction of days. If you want it in hours, just multiply by 24, which gives you, hours and fracition of hours
SELECT cdbl([Left] - [Arrived]) * 24 as worked_hours FROM yourtable
回答2:
DateDiff
DateDiff ( interval, date1, date2, [firstdayofweek], [firstweekofyear])
http://www.techonthenet.com/access/functions/date/datediff.php
DateDiff ("h", [Arrived], [Left])
回答3:
As Russell mentioned, use DateDiff
function. I tested on a test MsAccess DB and it works. Here is my select statement:
SELECT DateDiff('n',[Arrived],[Left])/60 AS Worked,
DateDiff('s',[Arrived],[Left])/3600 AS Worked2,
*
FROM Table1;
Sample Results:
Worked Worked2 Left Arrived
1 0.999722222222222 4/9/2013 3:00:00 PM 4/9/2013 2:00:01 PM
0.833333333333333 0.833055555555556 4/9/2013 3:00:00 PM 4/9/2013 2:10:01 PM
1.66666666666667E-02 2.77777777777778E-04 4/9/2013 3:00:00 PM 4/9/2013 2:59:59 PM
24 23.9997222222222 4/9/2013 3:00:00 PM 4/8/2013 3:00:01 PM
As you can see, the Worked2 approach (by second) is a bit more accurate.
来源:https://stackoverflow.com/questions/15910341/access-subtract-time