Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6026643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:28:31+00:00 2026-05-23T04:28:31+00:00

I am working on a stored proc and need last 9 months data. Need

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:28:32+00:00Added an answer on May 23, 2026 at 4:28 am

    You probably want GETDATE to calculate the nine month boundary from now:

    where t.date >= dateadd(m,-9, GETDATE())
    

    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

    declare @latest date
    select @latest = inserted.date
    delete from ... where t.date < dateadd(m, -9, @latest)
    

    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

    declare @today date;
    declare @firstOfMonth date;
    declare @nineMonthsAgo date;
    set @today = GETDATE();
    set @firstOfMonth = DATEADD(d, 1-DAY(@today), @today);
    set @nineMonthsAgo = DATEADD(m, -9, @firstOfMonth);
    
    ... WHERE date >= @nineMonthsAgo AND date < @firstOfMonth
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a rather hairy Stored Proc which returns XML (the XML
I'm working on a stored proc that executes some dynamic sql. Here's the example
Can someone provide a working example in which stored procedure returns a recordset and
I'm working on a pl sql stored procedure. What I need is to do
I am working on a stored proc (parent) that calls another stored proc (child).
In my application I need to call a Stored Proc Asynchronously. For this I
I want to write a stored proc which iterates through all of the tables
I've got a sql stored proc that is working fine in SSMS. When I
I have a function Split which I am calling in a stored proc with
I'm new to SQL and am now working with stored procedures. I was watching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.