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
 General SQL Server Forums
 New to SQL Server Programming
 call function

Author  Topic 

govindts
Starting Member

33 Posts

Posted - 2009-01-15 : 18:12:33
Sorry for asking very basic questions.. I am not a sqlserver guy.
I wanted to execute one userdefined function. How can i execute the function. I thought, i call this function through select statement.. But it did not work...
I just wanted to execute this function...

I tried

SELECT ADDBUSINESSDAYS(GETDATE(),2)

But it did not work. Any help is appreciated....



CREATE FUNCTION [dbo].[AddBusinessDays]
(
@StartDate DATETIME,
@NumberOfDays INT
)
RETURNS DATETIME
AS

/*
1 = Sonntag
2 = Montag
3 = Dienstag
4 = Mittwoch
5 = Donnerstag
6 = Freitag
7 = Samstag
*/

BEGIN
DECLARE @CurrentWeekday INT;
DECLARE @FullWeeks INT;

SET @CurrentWeekday = DATEPART(dw,@StartDate);

IF @CurrentWeekday = 2
BEGIN
SET @StartDate = DATEADD(d,4,@StartDate)
SET @NumberOfDays = @NumberOfDays - 4
END
IF @CurrentWeekday = 3
BEGIN
SET @StartDate = DATEADD(d,3,@StartDate)
SET @NumberOfDays = @NumberOfDays - 3
END
IF @CurrentWeekday = 4
BEGIN
SET @StartDate = DATEADD(d,2,@StartDate)
SET @NumberOfDays = @NumberOfDays - 2
END

IF @CurrentWeekday = 5
BEGIN
SET @StartDate = DATEADD(d,1,@StartDate)
SET @NumberOfDays = @NumberOfDays - 1
END

IF @NumberOfDays > 0
BEGIN
SET @FullWeeks = @NumberOfDays / 5
WHILE (@FullWeeks) > 0
BEGIN
SET @NumberOfDays = @NumberOfDays - 5
IF @NumberOfDays > 0
BEGIN
SET @StartDate = DATEADD(d,7,@StartDate)
END
ELSE
BEGIN
SET @StartDate = DATEADD(d,5,@StartDate)
END
SET @FullWeeks = @FullWeeks - 1
END
SET @StartDate = DATEADD(d,@NumberOfDays+2,@StartDate)
END

IF @NumberOfDays < 0
BEGIN
SET @StartDate = DATEADD(d,@NumberOfDays,@StartDate)
END

RETURN @StartDate;
END




tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-15 : 18:16:10
SELECT dbo.ADDBUSINESSDAYS(GETDATE(),2)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -