moe moe - 1 year ago 39
SQL Question

How do i get the last 12 months in sql

i have stored procedure that runs every month and i always want to run the rolling 12 months. I have 2 parameters in my stored procedure, start_date and end_date. For example, my start date and end date for this month should be like this:

declare @start_date date
declare @end_date date

set @start_date = '2016-08-01'
set @end_date = '2015-09-01'
exec myStoredProc
@DateBegin = @start_date
, @DateEnd = @end_date
, @DateType = 'DISCHARGE DATE'
, @ShowAllResultTables = 'N'

so my challenge is that i cannot get the start and end date automated so i can pull the rolling 12 months every month. I am hard coding now.

set @start_date = '2016-08-01'
set @end_date = '2015-09-01'

please help. thanks

Answer Source
Declare @Date Date,@DateR1 Date,@DateR2 Date

Set @Date=GetDate()
Set @DateR2=DateAdd(DAY,-Day(@Date)+1,@Date) 
Set @DateR1=DateAdd(MONTH,-13,@DateR2)

Select @DateR1,@DateR2


2015-08-01           2016-09-01