All Type Coding

Search Here

How to create a procedure that return back current month and next three month in sql server.

Create proc CurrentMonth_Next3Months
 as
declare @date date=getdate()
set @date = cast(cast(Month(@date) as varchar)+'/1/'+cast(year(@date) as varchar) as date)

declare @tbldate table (sl int identity(1,1),fldname varchar(255), flddate date)

insert into @tbldate values('--Select--','01/01/1988')


insert into @tbldate values(DateName(MM,DATEADD(MM,cast(month(@date) as int),-1)) +' '+ cast(Year(@date) as varchar),@date)

set @date =(select dateadd(MM ,1,cast(@date as date)))

insert into @tbldate values(DateName(MM,DATEADD(MM,cast(month(@date) as int),-1)) +' '+ cast(Year(@date) as varchar),@date)

set @date =(select dateadd(MM ,1,cast(@date as date)))

insert into @tbldate values(DateName(MM,DATEADD(MM,cast(month(@date) as int),-1)) +' '+ cast(Year(@date) as varchar),@date)
set @date =(select dateadd(MM ,1,cast(@date as date)))

insert into @tbldate values(DateName(MM,DATEADD(MM,cast(month(@date) as int),-1)) +' '+ cast(Year(@date) as varchar),@date)
 

select * from @tbldate






after executing the procedure Output will be like below
Exec
CurrentMonth_Next3Months

No comments :

Post a Comment