我有4个变量:
@startDate
@endDate
@maxCourses
@preferredCourseStartDay
我们说他们是:
Start Date - 1st Aug 2016
End Date - 30th Sept 2016
Max Courses - 5
Start Day - Tuesday
我需要从该日期范围的第一个星期二开始向临时表插入5行。
示例输出:
02/08/2016
09/08/2016
16/08/2016
23/08/2016
30/08/2016
感谢任何帮助
答案 0 :(得分:2)
这是一种使用CTE和窗口函数的方法:
.my-btn{
background-image:url(../img/template-1/read-more-closed.png);
background-repeat:no-repeat;
background-position:center;
width:50px;
height:50px;
margin-bottom:50px;
}
.open{
background-image:url(../img/template-1/read-more-open.png);
background-repeat:no-repeat;
background-position:center;
width:50px;
height:50px;
}
我毫不怀疑,在给定数据之后或之后找到第一个星期二有一种简洁而优雅的方式。此方法避开该方法,只查找生成的所有日期中的星期二。请注意,这假定您使用该语言的国际化设置。
答案 1 :(得分:0)
declare @startDate datetime = '8/1/2016'
declare @endDate datetime = '9/30/2106'
declare @maxCourses int = 5
declare @preferredCourseStartDay varchar(16) = 'Tuesday'
declare @wkDay int = case
when @preferredCourseStartDay = 'Sunday' then 0
when @preferredCourseStartDay = 'Monday' then 1
when @preferredCourseStartDay = 'Tuesday' then 2
when @preferredCourseStartDay = 'Wednesday' then 3
when @preferredCourseStartDay = 'Thursday' then 4
when @preferredCourseStartDay = 'Friday' then 5
when @preferredCourseStartDay = 'Saturday' then 6
end
SELECT DATEADD(DAY,number+1,dateadd(dd,@wkDay - datepart(weekday,@startDate),@startDate)) [Date]
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(DAY,number+1,dateadd(dd,@wkDay - datepart(weekday,@startDate),@startDate)) <= dateadd(day,@maxCourses,dateadd(dd,@wkDay - datepart(weekday,@startDate),@startDate))
答案 2 :(得分:0)
我不知道这些日子是每周还是连续,下面是如何处理这两种情况的示例。
pip3 install http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.zip