Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Number of Days Of Week
Hi,


I need to find the number of Mondays within a given date range.

Example:  How many Mondays between 01/01/2006 and 06/30/2006.

Any suggestions on how to do this?


Jim


Report this thread to moderator Post Follow-up to this message
Old Post
Jim.Mueksch@wellsfargo.com
10-29-06 12:14 AM


Re: Number of Days Of Week
Here is a function that will do this for you.

CREATE FUNCTION dbo.fnDaysBetween
(  @StartDate  datetime,
@EndDate    datetime,
@ConcernDay char(2)   -- MO, TU, WE, etc.
)
RETURNS int
AS
BEGIN

DECLARE @Calendar table
( CalendarDate smalldatetime )

WHILE ( @StartDate <= @EndDate )
BEGIN
INSERT INTO @Calendar
SELECT @StartDate
SET @StartDate = dateadd( day, 1, @StartDate )
END

RETURN
(  isnull(( 
SELECT DayCount
FROM ( SELECT 
DayCount = count(1), 
DayName  = datename( dw, CalendarDate )
FROM @Calendar
GROUP BY datename( dw, CalendarDate )
) dt
WHERE upper( left( DayName, 2 )) = @ConcernDay
), 0 )
)
END
GO


SELECT dbo.fnDaysBetween( '10-01-2006', '11-30-2006', 'mo' )
SELECT dbo.fnDaysBetween( '10-01-2006', '11-30-2006', 'fr' )
SELECT dbo.fnDaysBetween( '10-06-2006', '10-10-2006', 'fr' )
SELECT dbo.fnDaysBetween( '10-06-2006', '10-10-2006', 'we' )


DROP FUNCTION dbo.fnDaysBetween

-- 
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience. 
Most experience comes from bad judgment. 
- Anonymous

You can't help someone get up a hill without getting a little closer to the 
top yourself.
- H. Norman Schwarzkopf


<Jim.Mueksch@wellsfargo.com> wrote in message news:1161982262.413737.288480@k70g2000cwa.goo
glegroups.com...
> Hi,
> 
> 
>        I need to find the number of Mondays within a given date range.
> 
>        Example:  How many Mondays between 01/01/2006 and 06/30/2006.
> 
>        Any suggestions on how to do this?
> 
> 
> Jim
>

Report this thread to moderator Post Follow-up to this message
Old Post
Arnie Rowland
10-29-06 12:14 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

MS SQL Server archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 08:47 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006