How to show 12 months of data based on a selected date

Anonymous
2022-12-14T16:19:47.227+00:00

I have a query that works except for one thing i want it to show me data for the past 12 months based on a date a user selects in a ssrs report.`

`Declare @apl as Date = '1/1/2022' ;
Declare @prop as int = 8000 ;

--*/
SELECT
tblPropsFreeRent.intProp,
tblPropsFreeRent.dtMonth,
DATENAME(m, tblPropsFreeRent.dtMonth) + ' ' + DATENAME(yyyy, tblPropsFreeRent.dtMonth) AS strDate,

		intNumTimes AS intNumTimes,  
		tblPropsFreeRent.mnyAmount AS curAmount,  
		intMoveIns AS intMoveIns,  
		W.fltOcc AS fltOcc  
	FROM tblPropsFreeRent  
	LEFT JOIN SovranMisc.dbo.tblPropsLateFeeWaive W ON tblPropsFreeRent.intProp = W.intProp AND tblPropsFreeRent.dtMonth = W.dtMonth  
	WHERE  
	---tblPropsFreeRent.dtMonth = @Date  
	  
	DATEDIFF(Month,tblPropsFreeRent.dtMonth,GETDATE()) <=13  
	--tblPropsFreeRent.dtMonth >= DateADD(mm,DATEDIFF(mm,0,@Date)-12,0)  

and tblPropsFreeRent.intProp IN(@Prop);

SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,066 questions
Developer technologies | Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Naomi Nosonovsky 8,431 Reputation points
    2022-12-14T20:47:16.7+00:00

    In this case I think you can just use

    dateColumn >=dateadd(month, - 12, @apl )

    where @apl is the date selected from Date Picker.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-12-14T16:32:10.88+00:00

    (putyourdatecolumnhereOrBuildaDate)>= DATEadd(Month,-12,GETDATE())


  2. Naomi Nosonovsky 8,431 Reputation points
    2022-12-14T17:19:28.877+00:00
    tblPropsFreeRent.dtMonth>= DATEADD(Month,-12,CAST(GETDATE() as date))  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.