All Type Coding

Search Here

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

Create proc Month_Year
 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)
set @date =(select dateadd(MM ,1,cast(@date as date)))

select * from @tbldate order by sl desc

after executing the procedure Output will be like below
Exec Month_Year








No comments :

Post a Comment