Home > SQL Server > Get Months Name Between Two Dates in SQL

Get Months Name Between Two Dates in SQL


Hi all,

try this example to Get Months Name Between Two Dates in SQL


CREATE FUNCTION [dbo].[GetMonthList] (
 @StartDate DATETIME,
 @EndDate DATETIME
)
RETURNS @months TABLE (
 [month] VARCHAR(10)
)
WITH EXECUTE AS OWNER AS
BEGIN
 DECLARE @MonthDiff INT;
 DECLARE @counter INT;
 SET @counter = 0;
 SELECT @MonthDiff = DATEDIFF(mm, @StartDate, @EndDate);
 WHILE @counter <= @MonthDiff
 BEGIN
 INSERT @months
 Select DATENAME( month , DATEADD(mm, @counter, @StartDate))
 SET @counter = @counter + 1;
 END
 RETURN;
END

and to Test this Function try this

select * from dbo.GetMonthList ('2012-10-09','2012-12-09')

Hope this helps

Good Luck.

Advertisements
Categories: SQL Server
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: