我想在表格中添加一列来计算两个日期之间的周数,这两个日期在另外两列中。列是:
[DateStart] [date] NULL,
[DateEnd] [date] NULLNumOfWeeks,
[NumOfWeeks] [int]
如果结束日期为NULL,则计算结果应为StartDate与当前日期之间的周数。
干杯, 凯文。
答案 0 :(得分:1)
简单:
ALTER TABLE [table] ADD
[NumOfWeeks] AS DATEDIFF(week, DateStart, COALESCE(DateEnd, GETDATE())
答案 1 :(得分:0)
以下是Case方法也有效......
ALTER TABLE Timeslots ADD
[NumOfWeeks] AS DATEDIFF(week, DateStart, CASE when DateEnd IS NULL THEN GETDATE() Else DateEnd END)