user6628729
user6628729

Reputation: 323

get last 3 months on date selection

I have datepicker on form.aspx fromdate & to date i want when i select date from date picker then i want last 3 month record on the basic of datepicker selection i.e. if i select date from DEC then last 3 month record display

i try this

ALTER PROCEDURE [dbo].[SPMONTH]
@fromdate datetime,
@todate datetime
AS

select DATENAME(MONTH,tblReg.StartDate) AS [Month]
from tblReg
where
tblReg.StartDate>=DATEADD(MONTH,-3,GETDATE())
AND tblReg.EndDate<=GETDATE()
GROUP BY 
DATENAME(MONTH,tblReg.StartDate) 

when i execute this

[SPMONTH] '2016-01-01 00:00:00', '2016-01-30 23:59:59'

result

Month
August
July
June
May

above sp works but when i select date from aug then this show last 3 months and current month whereas when i select date from jan or another month then this shows july,june and may data whereas i want when i select jan or any other month suppose i select jan then want dec,nov,oct and jan

Upvotes: 1

Views: 2441

Answers (1)

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

You are not using passing parameters in your query i.e @fromdate and @todate not used in your query, Use this parameters in where clause as below

select DATENAME(MONTH,tblReg.StartDate) AS [Month]
from tblReg
where
tblReg.StartDate>=DATEADD(MONTH,-3, @fromdate)
AND tblReg.EndDate<= @todate
GROUP BY 
DATENAME(MONTH,tblReg.StartDate) 

Upvotes: 1

Related Questions