Friday, July 1, 2011

Display the Days Between the two dates ---- Procedure & Function


---------DAYS Procedure

CREATE PROCEDURE Proc_ShowDays(@SDate Datetime ,@Edate Datetime )
AS
BEGIN
DECLARE  @TAB TABLE ([DAYS] DATETIME )
DECLARE @I INT
DECLARE @CNT INT
SELECT @CNT= DATEDIFF(DD,@SDate,@Edate)
SET @I=0
WHILE (@I<=@CNT)
BEGIN
INSERT INTO @TAB SELECT DATEADD(DD,@I,@SDate)
SET @I=@I+1
END
SELECT * FROM @TAB
END


EXEC ShowDays '2011-06-01','2011-07-01'

-------Days Function

CREATE FUNCTION UDF_ShowDays (@SDate DATETIME ,@EDate DATETIME )
RETURNS @DAYS TABLE ([DAYS] DATETIME )
AS
BEGIN
DECLARE @I INT
DECLARE @CNT INT
SELECT @CNT= DATEDIFF(DD,@SDate,@Edate)
SET @I=0
WHILE (@I<=@CNT)
BEGIN
INSERT INTO @DAYS SELECT DATEADD(DD,@I,@SDate)
SET @I=@I+1
END
RETURN
END


SELECT * FROM UDF_DAY('2011-06-01' ,'2011-07-01')

No comments:

Post a Comment