0

Is there a way to have a column in the table which auto-calculates the time difference between the start date and the end date as such?

(datediff(hour,[StartTime],[EndTime]))
4

2 回答 2

5
ALTER TABLE yourTable
ADD yourColumn AS (datediff(hour,[StartTime],[EndTime]))

SQLFiddle 演示

编辑: 这是alter table的语法,当然你也可以在表创建期间定义它

CREATE TABLE tbl1 
(
    startDate DATETIME, 
    enddate DATETIME,
    diffCol AS (datediff(hour,startDate,enddate))
);

SSMS 在表设计器中还有一个选项,可以为计算列添加公式

于 2013-06-24T15:12:26.947 回答
1

假设 StartTime 和 EndTime 都是表同一行上的列,您可以使用计算列:

ALTER TABLE MyTable ADD TimeDiffHours AS DateDiff(hour,[StartTime],[EndTime]);
于 2013-06-24T15:13:26.277 回答