M.JAY M.JAY - 2 months ago 14
SQL Question

Sql how to get from 5 weeks startdate and end date

In SQL server, I'd like to display the date and time period for my reporting. The time period in the

WHERE
clause is below:

WHERE EventDate >= dateadd(wk, datediff(wk, 0, getdate()) -5, 0)
AND EventDate <= dateadd(wk, datediff(wk, 0, getdate()), 0)


how can I display this in the format: "26 Mar 2017 - 13 Apr 2017"

Answer

If I understand your question correctly, you want the period from 5 weeks ago to 5 weeks in the future.

You could try the following:

-- etc
WHERE EventDate BETWEEN dateadd(week,-5,getdate()) AND dateadd(week,5,getdate());

If you want to display the dates, you can try this:

SELECT format(dateadd(week,-5,getdate()),'d MMM yyyy') +' - '+ format(dateadd(week,5,getdate()),'d MMM yyyy');