Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2011-08-15 : 08:06:46
|
I'm doing a report in SQL and one part is busting my balls so I thought I'd do the decent thing and offer the challenge out to you clever bunch.I need to list the days of a given month on a Y axis (the up and down bit to the left), but there's a caveat - I only want to show FULL weeks (Monday to Sunday). So where the month starts part way through the week, I need it to show the last few days of the previous month for the Monday, Tuesday etc and then 1st of the selected month from whatever day it starts. The same should be done for the end of the month, i.e. displaying the start of the next month if its in the same week although this is less important.You can have parameters to work from, probably a start and end date which should be the first day and the last day in the selected month.A full week is Monday to Sunday.August isn't a good example of a month to do this as the first was a Monday, but taking June as an example, the desired output would have been:Mon 30/05/2011Tue 31/05/2011Wed 01/06/2011Thu 02/06/2011Fri 03/06/2011Sat 04/06/2011Sun 05/06/2011Mon 06/06/2011Tue 07/06/2011Wed 08/06/2011Thu 09/06/2011Fri 10/06/2011Sat 11/06/2011Sun 12/06/2011Mon 13/06/2011Tue 14/06/2011Wed 15/06/2011Thu 16/06/2011Fri 17/06/2011Sat 18/06/2011Sun 19/06/2011Mon 20/06/2011Tue 21/06/2011Wed 22/06/2011Thu 23/06/2011Fri 24/06/2011Sat 25/06/2011Sun 26/06/2011Mon 27/06/2011Tue 28/06/2011Wed 29/06/2011Thu 30/06/2011Fri 01/07/2011Sat 02/07/2011Sun 03/07/2011Here's your parameter declarations to get you started:DECLARE @StartDate DATETIMEDECLARE @EndDate DATETIMESET @StartDate = '2011-06-01'SET @EndDate = '2011-06-30' I'd be chuffed to bits if someone could help out with this. Many thanks.Nick.---------------------------------------------------------------------------------http://www.mannyroadend.co.uk A Bury FC supporters website and forum |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-08-15 : 08:23:03
|
for calculating the revised start date:SELECT DATEADD(day,-(DATEDIFF(day,'19000101',@StartDate))%7,@startDate) AS NewStartDate; |
 |
|
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2011-08-15 : 12:34:06
|
| Works a treat that, thanks a lot. Help yourself to a biscuit.---------------------------------------------------------------------------------http://www.mannyroadend.co.uk A Bury FC supporters website and forum |
 |
|
|
|
|
|
|
|