Click here to Skip to main content
16,018,234 members
Please Sign up or sign in to vote.
3.40/5 (3 votes)
Hello all,

I have a situation in which I need to display list of months with years between two dates saved in database i.e BeginDate and Enddate. I researched and found a SQL function that displays months between two dates but I have no idea about obtaining months/year at same time.
Ex: Begin Date(MM/YY/YEAR): 03/02/2012 EndDate : 10/01/2012

I should get output as March/2012,April/2012,May/2012 ....October/2012. or it can be 03/2012,04/2012,05/2012...10/2012. Ultimately I would like to display this output in a dropdown dynamically.

Here is the function I have modified that displays only months i.e March,April,May...October for above dates. I would appreciate if you could help me with this regard.
SQL
	ALTER PROCEDURE dbo.Addmonthslist
	
	@BeginDate AS Datetime,
	@EndDate AS Datetime,
	@ProjectID AS varchar(50)
AS
SELECT
 BeginDate,
 EndDate
 from tblProjects where ProjectID = @ProjectID
 

SELECT  DATENAME(MONTH, DATEADD(MONTH, x.number, @BeginDate)) AS MonthName
FROM    master.dbo.spt_values x
WHERE   x.type = 'P'
AND     x.number <= DATEDIFF(MONTH, @BeginDate, @EndDate);
Posted
Updated 8-May-13 21:31pm
v3

1 solution

SQL
ALTER PROCEDURE dbo.Addmonthslist
	
	@BeginDate AS Datetime,
	@EndDate AS Datetime,
	@ProjectID AS varchar(50)
AS
SELECT
 
 BeginDate,
 EndDate
 from tblProjects where ProjectID = @ProjectID
 
 
SELECT  DATENAME(MONTH, DATEADD(MONTH, x.number, @BeginDate)) + '-' + Convert(varchar(4), Year(DATEADD(MONTH, x.number, @BeginDate))) AS MonthName
FROM    master.dbo.spt_values x
WHERE   x.type = 'P'
AND     x.number <= DATEDIFF(MONTH, @BeginDate, @EndDate);
	
	RETURN

Happy Coding!
:)
 
Share this answer
 
v2
Comments
Slacker89 9-May-13 3:38am    
Thanks for your quick solution. It works like charm . You are awesome. How should I display this result in a dropdown. I tried using SQL dataadapter but it gave me an error since I am not declaring the output result anywhere. Please help me :(
Aarti Meswania 9-May-13 4:24am    
Welcome!
Glad to help you! :)
Aarti Meswania 9-May-13 4:35am    
take output in a datatable
and then assign datatable to dropdown's datasource property
and dropdown.DisplayMember="ColumnName" e.g. "MonthName" is your column name here
then Dropdown.Bind()
Slacker89 9-May-13 19:13pm    
I tried as you mentioned but I cannot get Displaymember as a property for dropdown. Here is my code, I used datatext and datavalue property( I know its wrong) since displaymember is not working. Please have a look and correct it. Appreciate your time and patience.
Error I got "DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'MonthName'."
private void Monthdata()
{
var str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
var conn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("Addmonthslist", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ProjectID", SqlDbType.VarChar).Value = ddprojectid.SelectedItem.Value;
cmd.Parameters.Add("@BeginDate", SqlDbType.VarChar).Value = ddbegin.Text;
cmd.Parameters.Add("@EndDate", SqlDbType.VarChar).Value = ddend.Text;
var adap = new SqlDataAdapter(cmd);
DataTable myDataTable = new DataTable();
adap.Fill(myDataTable);
ddmonth.DataSource = myDataTable;
ddmonth.DataTextField = "MonthName";
ddmonth.DataValueField = "MonthName";
ddmonth.DataBind();
}
Aarti Meswania 10-May-13 0:39am    
check column name in "MyDataTable"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900