Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have two dates like 1. 01/01/2014
and 2. 01/03/2014

I have to get output JAN,FEB AND MAR
Posted
Comments
Shweta N Mishra 31-Oct-14 6:28am    
Hi Asis, Mark the Solution to be accepted to get the question out of Un-answered list.
BillWoodruff 31-Oct-14 12:47pm    
I am curious: are you asking for a solution in SQL, or in C#; it's very easy to do this in C#.

Try this-


SQL
DECLARE @Date1 DATETIME
DECLARE @Date2 DATETIME

SET @Date1='2014-01-01'
SET @Date2='2014-03-01'

DECLARE @Result VARCHAR(100)
SET @Result=''
WHILE (@Date1<=@Date2)
BEGIN
    IF(@Result!='') SET @Result+=','
    SELECT @Result+=UPPER(LEFT(DATENAME(MONTH,@Date1),3))
    SET @Date1=DATEADD(MONTH,1,@Date1)
END

SELECT @Result
 
Share this answer
 
In Addition to Shweta Nikhil Mishra
I have update the query
SQL
Declare @FromDate Date='01/01/2014'
Declare @ToDate Date='03/01/2014'

 create table #temp( MonthsName varchar(50));
While @ToDate>=@FromDate
BEGIN


insert into #temp values(DateName(MM,@FromDate))
set @FromDate=DateAdd(mm,1,@FromDate)

END

select * from #temp
drop table #temp
 
Share this answer
 
Try This

SQL
Declare @FromDate Date='01/01/2014'
Declare @ToDate Date='03/01/2014'

While @ToDate>=@FromDate
BEGIN

Select DateName(MM,@FromDate)

set @FromDate=DateAdd(mm,1,@FromDate)

END
 
Share this answer
 

Just a small logic ---->

SQL
CREATE TABLE #MONTH (MonthName VARCHAR(20))

DECLARE @StartMonth INT = (SELECT DATEPART(MONTH, '01/01/2014'))--GET START MONTH 
DECLARE @EndMonth INT = (SELECT DATEPART(MONTH, '04/01/2014'))--GET END MONTH 

WHILE (@StartMonth<=@EndMonth) --From start month to end month
BEGIN
INSERT INTO #MONTH SELECT DATENAME(MONTH,CAST (@StartMonth  AS VARCHAR(2))+'/01/2014')
SET @StartMonth=@StartMonth+1;
END

----------------------------------
DROP TABLE #MONTH
 
Share this answer
 
v2

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