I am working on a stored proc and need last 9 months data. Need a syntax Which will automatically deletes the oldest data when new data will be added to the table(Latest 9 months data).
That syntax is gonna be used in Select Syntax.
I have used
select * from tablename t
left outer join calendartable r on
t.fiscal_month=r.fiscal_month
where t.date > dateadd(m,-9,date)
I know it is wrong. Could you guys please help me with this.
Thanks
You probably want GETDATE to calculate the nine month boundary from now:
Beware that if t.date is a date and time field not just date you’ll see odd behaviour on the nine-month boundary unless you also round away the time before the comparison.
Or if you’re comparing it against another value e.g. the date of the inserted record in your trigger then what you’ve got is probably OK, e.g. something like
although I suggest you actually archive off the data, not delete it.
Since you’ve clarified you want whole months, i.e. 9 months from the end of last month, you could use