Friday, July 1, 2011

Display the List of Months Between the two dates ---- Procedure & Function


------- List of Months  Procedures

CREATE PROCEDURE Proc_ListOfMonths(@SDate DATETIME,@EDate DATETIME)
AS
BEGIN
DECLARE @I INT
DECLARE @J INT
DECLARE @TAB TABLE (Months DATETIME)
SET @I = 0
SELECT @J = DATEDIFF(MM,@SDate,@EDate)
WHILE (@I<=@J)
BEGIN
INSERT INTO @TAB SELECT DATEADD(MM,@I,@SDate)
SET @I = @I +1
END
SELECT DATEPART(MM,MONTHS) Months  FROM @TAB
END

EXEC Proc_ListOfMonths '2010-06-30 ','2011-06-30'



-----------List of Months Function

CREATE FUNCTION UDF_ListOfMonths (@SDate DATETIME, @EDate DATETIME )
RETURNS @Months TABLE (Months DATETIME )
AS
BEGIN
DECLARE @I INT
DECLARE @J INT
SET @I = 0
SELECT @J = DATEDIFF(MM,@SDate,@EDate)
WHILE (@I<=@J)
BEGIN
INSERT INTO @Months SELECT DATEADD(MM,@I,@SDate)
SET @I = @I +1
END
RETURN
END


SELECT DATEPART(MM,Months)Months FROM UDF_ListOfMonths ('2010-06-30 ','2011-06-30')

2 comments:

  1. Been looking for a few trendy pieces of clothes for my daughter,
    it seems like fashion for any under 5's is seriously lacking style!


    my homepage Adrienne

    ReplyDelete
  2. Hey There. I found your blog using msn. This is a very well written article.
    I'll make sure to bookmark it and return to learn more of your helpful info.
    Thank you for the post. I will definitely return.


    My web site Vanita

    ReplyDelete