1

我有一个sql表名StudentInfo,其中数据为

StudentID   Age startDate   EndDate

1       14  5/05/2013   7/05/2013   
4       17  4/04/2012   8/10/2012

我想为此表创建一个视图,在其中添加一个名为 total days 的列,显示 StartDat 和 Enddate 之间的天数。就像我希望看到的结果一样

StudentID   Age startDate   EndDate     TotalDays

1       14  5/05/2013   7/05/2013   3
4       17  4/04/2012   8/04/2012   5
4

1 回答 1

5

您可以datediff用来计算天数:

create view dbo.vw_StudentInfo
as
select  StudentID
,       Age
,       StartDate
,       EndDarte
,       datediff(day, StartDate, EndDate) as TotalDays
from    dbo.StudentInfo
于 2013-05-11T11:20:04.337 回答