declare @ownerId int = ; declare @ownerType varchar(3) = ; declare @startMonth datetime = ; declare @endMonth datetime = ; select opportunityId, opportunityidtk as opportunitytk, cases, lbs, dollars, cases as equivunits, stagename, likelihoodpercent, format( estorderdate, 'MM/dd/yyyy' ) as estorderdate, partnername, effectiverepfirstname + ' ' + effectivereplastname as salesrepfullname, partnertype, partnerid, 0 partnertk, territoryname, null accountownerfullname, partnerterritoryname as accountterritoryname from tbl_DW_Opportunities#_tablesuffix# with (nolock) where ownerId = @ownerId and ownerType = @ownerType and partnerTerritoryId in ( ) and effectiveSalesRepId in ( ) and partnerTerritoryId in ( ) and effectiveSalesRepId in ( ) and ( 'P' + convert( varchar, likelihoodpercent * 100 )) = and statusCode = and estOrderDate >= getDate() and estOrderDate between @startMonth and @endMonth for json auto, include_null_values