Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Frnds,
I need to Following outputs.
I am having table
Col1: From Date col2: To Date

01-Jan-2012 01-Jan-2013


I need to get output as

MonthWise Date
Jan-2012
Feb-2012
Mar-2012
.
.
.
.
.
Nov-2012
Dec-2012
Jan-2013
Posted

SELECT SUBSTRING(CONVERT(VARCHAR(11), GETDATE(), 113), 4, 8 will get you the month and year of a date. You can replace GETDATE() with your date column
 
Share this answer
 
Comments
itsureshuk 20-Dec-12 1:45am    
I need the output of months between from Date -- to Date(as mention above output)
Christian Graus 20-Dec-12 2:02am    
So write SQL to filter on the dates. That's easy
Try
SQL
DECLARE @Sample TABLE
(
	FromDate DATETIME,
	ToDate DATETIME
)


INSERT INTO @Sample
SELECT '01-Jan-2012', '01-Jan-2013'

DECLARE @FromDate DATETIME
DECLARE @ToDate DATETIME

SELECT @FromDate = FromDate, @ToDate = ToDate FROM @Sample

;WITH CTE AS
(
	SELECT 0 AS n, @FromDate AS FromDate
	UNION ALL
	SELECT n+1 AS n, DATEADD(month,1,FromDate)
	FROM CTE 
	WHERE FromDate < @ToDate
)

SELECT REPLACE(SUBSTRING(CONVERT(VARCHAR(11), FromDate, 113), 4, 8),' ','-') AS [MonthWise Date]  FROM CTE
 
Share this answer
 
Comments
itsureshuk 20-Dec-12 2:03am    
Thk u.Actually What i expected is getting from your code...I try to Implement in my tables.I accept ur solution

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