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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Function

Author  Topic 

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2009-09-29 : 12:26:56
Dear All,
I am new to SQL server programming
i need to write a function that returns the interger
and accepts the 2 dates between those 2 days i need to count the number of days Except the week ends.

Can somebody help me.

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2009-09-29 : 12:47:10
Dear All,
Pls let me know how to execute the SQL server Function ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-29 : 12:58:01
[code]CREATE FUNCTION GetWorkingDays
(@Start datetime,
@End datetime
)
RETURNS int
AS
BEGIN
DECLARE @WorkDays int
SELECT @WorkDays=COUNT(1)
FROM
(
SELECT DATEADD(dd,number,@Start) AS Date
FROM master..spt_values
WHERE type='p'
AND DATEADD(dd,number,@Start)<=@End
AND DATENAME(dw,DATEADD(dd,number,@Start)) NOT IN ('Saturday','Sunday')
)t

RETURN @WorkDays
END

execute it like

SELECT dbo.GetWorkingDays('01-03-2009','01-07-2009')
[/code]
Go to Top of Page
   

- Advertisement -