Thursday, April 03, 2008

SQL Function EventConflict


CREATE FUNCTION dbo.EventConflict(
@newEventDate datetime,
@newEventTime datetime,
@newEventLength datetime,
@newLocation int
)
RETURNS int
AS
BEGIN
DECLARE @result int
SET @result = (SELECT Count(*) FROM tbl_events
WHERE
DATEDIFF(d,eventDate,@newEventDate)=0
AND locationid = @newLocation
AND ABS(DATEDIFF(mi, DATEADD(mi, 0, eventTime), DATEADD(mi, 0, @newEventTime))) < DATEDIFF(mi,0,@newEventLength)
AND ABS(DATEDIFF(mi, DATEADD(mi, DATEDIFF(mi,0,CONVERT(datetime,eventLength,108)), eventTime),
DATEADD(mi, DATEDIFF(mi,0,CONVERT(datetime,@newEventLength,108)), @newEventTime))) < DATEADD(mi,0,@newEventLength)
)
RETURN @result
END

1 comment:

Dave said...

its a user defined function isnt it? :) what was the question? dt