M.JAY M.JAY - 1 year ago 73
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

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 Source

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');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download