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 |
jhaney
Starting Member
33 Posts |
Posted - 2011-03-08 : 11:09:12
|
I have a stat report that pulls appointments by date. select * from appointments where date between @startdate and @enddateI need to find a way to run to run this report for the first full week of the month (M-F) without having to enter the dates? Any ideas?ThanksJoshua |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-03-08 : 12:07:36
|
This is from Visakh, you should be able to adapt it to your needsIF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME ='GetNthWeekday' AND ROUTINE_SCHEMA = 'dbo' AND ROUTINE_TYPE='FUNCTION')DROP FUNCTION dbo.GetNthWeekdayGO CREATE FUNCTION dbo.GetNthWeekday(@date datetime,--Any date of month under consideration@N int,-- Nth occurance@WeekDay int--Which week day to look for (Sunday-1,Monday-2,..,Saturday-7)--returns NULL when calculated date is out of scope of month)RETURNS datetime ASBEGINDECLARE @StartDate datetime,@ResultDate datetimeSET @StartDate=DATEADD(mm,DATEDIFF(mm,0,@date),0)SELECT @ResultDate=DATEADD( dd, CASE WHEN DATEPART(DW,DATEADD(wk,@N-1,@StartDate)) > @WeekDay THEN (7 - (DATEPART(DW,DATEADD(wk,@N-1,@StartDate))- @WeekDay)) ELSE (@WeekDay - DATEPART(DW,DATEADD(wk,@N-1,@StartDate))) END, DATEADD(wk,@N-1,@StartDate))SET @ResultDate= CASE WHEN MONTH(@ResultDate) <> MONTH(@date) THEN NULL ELSE @ResultDate END RETURN @ResultDateENDJimEveryday I learn something that somebody else already knew |
 |
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2011-03-08 : 12:12:46
|
Its a bit long winded but it works.DECLARE @mStart DATETIMESET @mStart = DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0)DECLARE @mEnd DATETIMESET @mEnd = DATEADD(MONTH,1,@mStart)CREATE TABLE #Dates (tDate DATETIME)WHILE @mStart < @mEndBEGIN INSERT INTO #Dates (tDate) SELECT @mStart SET @mStart = DATEADD(DAY,1,@mStart)END--SELECT tDate,DATEPART(WEEKDAY,tDate) as DayNo--FROM #Dates DECLARE @startdate DATETIMEDECLARE @enddate DATETIMESELECT @startdate = MIN(tDate) FROM #Dates WHERE DATEPART(WEEKDAY,tDate) = 2SELECT @enddate = MIN(tDate) FROM #Dates WHERE DATEPART(WEEKDAY,tDate) = 6 AND tDate > @startdateSELECT @startdate,@enddateDROP TABLE #Dates ---------------------------------------------------------------------------------http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club |
 |
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2011-03-08 : 12:15:35
|
nb: Prob should have mentioned but I'm assuming that you have your SQL Server set up so that your dayofweek starts with Sunday. If its not then you should change that part of the query.---------------------------------------------------------------------------------http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club |
 |
|
jhaney
Starting Member
33 Posts |
Posted - 2011-03-08 : 13:44:55
|
Thanks Guys I'll give it a shot. |
 |
|
jhaney
Starting Member
33 Posts |
Posted - 2011-03-08 : 14:02:00
|
Trying now to get those dates into my select statement below select * from apptointmentswhere company = 'mycompany'and adate between @startdate and @enddateThanks in advance |
 |
|
|
|
|
|
|