Hi Everyone,
I'm working on a procedure that is used to calculate data in weekly and monthly, currently I can use datepart function to extract the week number/month from given date (as the SQL below). The week number/month, extracted from previous step, will be put into a loop for calculating purpose. And we need to determine the start date and end date of specific week/month, could you please share me how get those date from given week number or month ?
SELECT @min_date_time = min(my_date) FROM my_table
--Minimum year in database
SELECT @min_year = datepart(yy,min(my_date)) FROM my_table
--Minimum month in database
SELECT @min_month = datepart(mm,min(my_date)) FROM my_table
--Minimum week in database
SELECT @min_week = datepart(wk,min(my_date)) FROM my_table
SELECT @curr_date_time = getdate()
--Current year
SELECT @curr_year = datepart(yy, @curr_date_time)
--Current month
SELECT @curr_month = datepart(mm, @curr_date_time)
--Current week
SELECT @curr_week = datepart(wk, @curr_date_time)
WHILE @min_week <= @curr_week
BEGIN
@start_date_of_week = ???
@end_date_of_week = ???
/*Do Some Logic*/
SELECT @min_week = @min_week + 1
END
WHILE @min_month <= @curr_month
BEGIN
@start_date_of_month = ???
@end_date_of_month = ???
/*Do Some Logic*/
SELECT @min_month = @min_month + 1
END
P/S: I also have similar logic for Oracle too, and I haven't found a solution yet. If you can help me on that, I really appreciate
Thanks
Khoa Tran