Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I used four dropdowmlist,
for from range i use one dropdown(month) and one dropdown(year)
for to range i use one dropdown(month) and one dropdown(year)
when i select
from range and to range
for example
jan2013
may2013
means

output should be
jan2013
feb2013
mar2013
apr2013
may2013

is it possible?
any suggestion pls................!
Posted
Comments
♥…ЯҠ…♥ 12-Dec-13 2:00am    
You have dropdownlist in c# and you want it in SQL? where do you want to display?
prabhatsp 12-Dec-13 2:12am    
sql itself k

Have a look at example:
SQL
DECLARE @startdate DATETIME
DECLARE @enddate DATETIME

SET @startdate = '2013-01-01'
SET @enddate = '2013-05-01'

;WITH Months AS
(
	SELECT @startdate AS MyDate
	UNION ALL
	SELECT DATEADD(mm,1,MyDate) AS MyDate
	FROM Months
	WHERE DATEADD(mm,1,MyDate) <= @enddate
)
SELECT CONVERT(VARCHAR(30), MyDate, 121) AS MyMonths, LEFT(DATENAME(MONTH ,MyDate),3) + CONVERT(VARCHAR(4),YEAR(MyDate)) AS MyMonths1
FROM Months


Result:
MyMonths                MyMonths1
2013-01-01 00:00:00.000 Jan2013
2013-02-01 00:00:00.000 Feb2013
2013-03-01 00:00:00.000 Mar2013
2013-04-01 00:00:00.000 Apr2013
2013-05-01 00:00:00.000 May2013


Above query uses CTE[^], but it is possible to achieve that using DO...WHILE[^] loop.
 
Share this answer
 
Comments
Simon_Whale 12-Dec-13 5:39am    
+5 nice solution.

I think the recursive CTE fits it well :)
Maciej Los 12-Dec-13 5:40am    
Thank you, Simon ;)
SQL
DECLARE @StartDate  DATETIME,
        @EndDate    DATETIME;

SELECT   @StartDate = '20120101'        
        ,@EndDate   = '20130805';


SELECT  convert(varchar,DATENAME(MONTH, DATEADD(MONTH, x.number, @StartDate))) +convert(varchar,DATENAME(YEAR , DATEADD(MONTH , x.number, @StartDate))) AS MonthName
FROM    master.dbo.spt_values x
WHERE   x.type = 'P'        
AND     x.number <= DATEDIFF(MONTH, @StartDate, @EndDate);


Reference: []
I hope this will be useful for you.
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 12-Dec-13 3:36am    
this code works fine .. Good.

But please explain about the master.dbo.spt_values
prabhatsp 12-Dec-13 5:49am    
thk u, i got the output

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