Hello Experts !
I need support to get the function to add ... The sample table creates the date and select statement required results .Suppose in the second column we have WkDate which starts from Thrusday to wednesday works ok .
- Second column i have WkDatelast required last date of the week , the week started from Thursday and end with Wednesday ( required every date of Wednesday retrieve in this column).
- Third column Week to Date Suppose i have a date 2020-10-29 00:00:00.000 which is Thrusday and i want to run sql statement till 2020-10-30 00:00:00.000 so the date 2020-10-30 00:00:00.000 retrive in this column that the statement run till 2020-10-30 00:00:00.000 ( called Week to date)... Please help to solve all the comment out columns.
---Drop TABLE Dates
CREATE TABLE Dates (
START_DATETIME DATETIME,
PRIMARY KEY (START_DATETIME)
)
DECLARE @dIncr DATETIME = CONVERT(datetime, '2019-01-1 00:00:00.000')
DECLARE @dEnd DATETIME = CONVERT(datetime, '2020-10-31 00:00:00.000')
WHILE ( @dIncr < @dEnd )
BEGIN
INSERT INTO Dates (START_DATETIME) VALUES( @dIncr )
SELECT @dIncr = DATEADD(DAY, 1, @dIncr )
END
SELECT
[Date] = START_DATETIME --ISNULL make this column NOT NULL
/* Refelect Date Periods */
,WKDate = DATEADD(dd,DATEDIFF(dd,3,START_DATETIME)/7*7,3)--- starts from Thrusday to Wednesday
-- ,WKDateLast = --- End on Wednesday
-- ,WeekToDate = ---starts from Thrusday Week to date
,WKDate2 = DATEADD(day, DATEDIFF(day, 0, START_DATETIME) - (DATEPART(weekday, START_DATETIME + @@DATEFIRST - 1 ) - 1), 0) --- starts from Monday to Sunday
-- ,WKDate2Last = -------- End date Sunday
-- ,WeekToDate2 = ---starts from Monday Week to date
,MMDate = DATEADD(mm,DATEDIFF(mm,0,START_DATETIME),0) ---First date of the month
,MMDateLast = DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,0,START_DATETIME)+1,0)) ---Last date of the month
,MonthToDate = ---Last from Ist date of the month to date
,QQDate = DATEADD(qq,DATEDIFF(qq,0,START_DATETIME),0) ---First date of the Quater
,QQDateLast = DATEADD(d, -1, DATEADD(q, DATEDIFF(q, 0, START_DATETIME) +1, 0)) ---Last date of the Quater
--- ,QuaterToDate = ---First date of the Quater to Date
,YYDate = DATEADD(yy,DATEDIFF(yy,0,START_DATETIME),0) --First date of Year
,YYDateLast = DATEADD (dd, -1, DATEADD(yy, DATEDIFF(yy, 0, START_DATETIME) +1, 0)) --Last date of Year
,YearToDate = --First date of Year to date
,CumDate = DATEADD(dd, DATEDIFF(dd, 0, START_DATETIME), 0)
,fiscalDate = DATEADD(dd,0, DATEDIFF(dd,0, DATEADD( mm,-(((12 + DATEPART(m, START_DATETIME)) - 7)%12), START_DATETIME ) - datePart(d,DATEADD( mm, -(((12 + DATEPART(m, START_DATETIME)) - 7)%12),START_DATETIME ))+1 ) ) --- First date of the Fiscal Year
-- ,fiscalDateEnd = --- End date of the Fiscal Year
-- ,fiscalToDate = ----- First date of the Fiscal Year to date
from Dates