Kingston Dhasian - Thursday, February 1, 2018 11:14 PM
I need help with this currently I do it like this :
Input :
CT_PROM_START | CT_PROM_END |
4-Jan-18 | 19-Jan-18 |
3-Feb-18 | 17-Feb-18 |
SQL:
Declare @beginDate Datetime, @EndDate Datetime
Select @beginDate = '01/03/2015', @EndDate = '31/12/2018'
create Table #cal
(CalendarDate Datetime Primary key, IsWeekend Bit, YearNo SmallInt, QuarterNo TinyInt, MonthNo TinyInt, DayOfYearNo SmallInt, DayNo TinyInt, WeekNo TinyInt, WeekDayNo TinyInt)
While @beginDate <= @endDate
Begin
Insert Into #cal
Select
@beginDate As CalendarDate
,(Case When DATEPART(Weekday, @beginDate) In (7, 1) Then 1 Else 0 End) As IsWeekend
,DATEPART(Year, @beginDate) As YearNo
,DATEPART(QUARTER, @beginDate) As QuarterNo
,DATEPART(MONTH, @beginDate) As MonthNo
,DATEPART(DayOfYear, @beginDate) As DayOfYearNo
,DATEPART(Day, @beginDate) As DayNo
,DATEPART(Week, @beginDate) As WeekNo
,DATEPART(WEEKDAY, @beginDate) As WeekDayNo
Set @beginDate = DateAdd(Day, 1, @beginDate)
End
--drop table #cal
select a.*,b.* from temp_willowton16Jan18 a left join #cal b on b.CalendarDate>=a.[ct_prom_start] and b.CalendarDate<=a.[ct_prom_end]
where IsWeekend = 1 and weekdayno=7
Current Output :
CT_PROM_START | CT_PROM_END | Promo Weekending Saturday |
4-Jan-18 | 19-Jan-18 | 06-Jan-18 |
4-Jan-18 | 19-Jan-18 | 13-Jan-18 |
3-Feb-18 | 17-Feb-18 | 03-Feb-18 |
3-Feb-18 | 17-Feb-18 | 10-Feb-18 |
3-Feb-18 | 17-Feb-18 | 17-Feb-18 |
my code is always either missing the last Saturday or not stopping on the Saturday it should stop on.
Desired Output:
CT_PROM_START | CT_PROM_END | Promo Weekending Saturday |
4-Jan-18 | 19-Jan-18 | 06-Jan-18 |
4-Jan-18 | 19-Jan-18 | 13-Jan-18 |
4-Jan-18 | 19-Jan-18 | 20-Jan-18 must duplicate to this too because Promotion ends in that week that ends on the 20 Jan 2018 |
3-Feb-18 | 17-Feb-18 | 03-Feb-18 |
3-Feb-18 | 17-Feb-18 | 10-Feb-18 |
3-Feb-18 | 17-Feb-18 | 17-Feb-18 |
Please can anyone help.