计算偏移量的开始日期和结束日期之间的天数(Calculate days between Start and End date at an offset)
我在SQL中使用以下列创建了一个Calendar Table:
现在我有一个开始日期,结束日期和偏移量,并且必须计算天数,如下所述:
start date: 1-12-2016----End date: 10-12-2016----Offset: 2输出:
1-12-2016 3-12-2016 5-12-2016 7-12-2016 9-12-2016我似乎无法想到这个问题的查询。 在这方面的任何帮助将受到高度赞赏。
I have created a Calendar Table in SQL with these columns:
Now I have a start date, end date and an offset, and have to calculate days as explained below:
start date: 1-12-2016----End date: 10-12-2016----Offset: 2Output:
1-12-2016 3-12-2016 5-12-2016 7-12-2016 9-12-2016I cant seem to be able to think a query for this. Any help in this regard will be highly appreciated.
最满意答案
使用模运算:
select c.date from calendar c where c.date >= @startdate and c.date <= @enddate and datediff(day, @startdate, c.date) % @offset = 0Use modulo arithmetic:
select c.date from calendar c where c.date >= @startdate and c.date <= @enddate and datediff(day, @startdate, c.date) % @offset = 0更多推荐
发布评论