Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 3019

Re: How can I get the start date & end date of given week number or month

$
0
0

Hi All,

 

Sorry for my late response, it took a week for me and my colleague to find solution (googled also ). That may not a best solution but we can handle the necessary logic at this time. Below is our solution for calculating start date and end date for specific week number.

 

---------------Start Date & End Date for Given Week Number-------------

IF (week_count = min_week AND year_count = min_year)

 

     --first week will not be add 7 days

 

    SELECT next_date_week = dateadd(dd, 0, min_date_time)

 

ELSE IF (week_count = 1)

 

    SELECT start_date = CONVERT(DATETIME, CONVERT(VARCHAR(4), year_count) + '-1-1')

 

ELSE

 

     -- add 7 days to get the next week

 

     SELECT next_date_week = dateadd(dd, 7, next_date_week)

 

 

 

SELECT dw_number = datepart(dw, next_date_week)  --get day of week number

 

 

SELECT to_first_dw = (dw_number -1) * -1,  --get number of days to subtract to get week start

 

 

to_last_dw = 7 - dw_number          --get number of days to add to get week end

 

 

--Get the start date and end date of week_count, ready for calculate chart data

 

SELECT start_date = dateadd(dd,to_first_dw, next_date_week),

 

        end_date = dateadd(dd,to_last_dw, next_date_week)

 

 

Thanks

Khoa Tran


Viewing all articles
Browse latest Browse all 3019

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>