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

  • SEARCH
  • Home
  • 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 8307613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:33:45+00:00 2026-06-08T18:33:45+00:00

I have a table that contains only Month and Year field both as int.

  • 0

I have a table that contains only Month and Year field both as int.
Given an input say month, 6 months.
I need to project the data that are 6 months old.

for example I have data from 2001 , 1st Janaury till date. Given input as 6 months , so I have to go back 6 months
from cuurent date to get the result .

Output will be Data From Jan 2001 to Jan 2012.

I have done the program using the right,substring, string comparison.(a nasty hack)

Is there any prominent way of doing so?

DDL

Declare @t table (Mnth int, Yr int)
Insert Into @t 

-- 2011
Select 1,2011 Union All select 2,2011 Union All Select 3, 2011 Union ALL Select 4,2011 Union ALL
Select 5,2011 Union All select 6,2011 Union All Select 7, 2011 Union ALL Select 8,2011 Union ALL
Select 9,2011 Union All select 10,2011 Union All Select 11, 2011 Union ALL Select 12,2011 Union ALL

--2012
Select 1,2012 Union All select 2,2012 Union All Select 3, 2012 Union ALL Select 4,2012 Union ALL
Select 5,2012 Union All select 6,2012 Union All Select 7, 2012 Union ALL Select 8,2012 Union ALL
Select 9,2012 Union All select 10,2012 Union All Select 11, 2012 Union ALL Select 12,2012

Declare @inputMonth int = 6

I am trying without string conversion

Select Mnth,Yr,YEAR(DATEADD(mm,-@inputMonth,getdate())),MONTH(DATEADD(mm,-@inputMonth,getdate()))
From @t
WHERE   YEAR(DATEADD(mm,-@inputMonth,getdate())) < Yr
AND MONTH(DATEADD(mm,-@inputMonth,getdate())) < Mnth

But it is not working

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-06-08T18:33:46+00:00Added an answer on June 8, 2026 at 6:33 pm

    You could always convert to datetime and compare that.

    Something like:

    WHERE
        CONVERT(Datetime, 
            CONVERT(nvarchar(4), YearPart) + '-' + CONVERT(nvarchar(2), MonthPart) + '-01')
        ) > DATEADD(month, -6, GETUTCDATE())
    

    i.e. concatenate your two columns to yield CONVERT(Datetime, "2011-1-01"), for instance.

    As this answer to another question points out, ify ou don’t want to do string conversions, you would have to add to do a series of DATEADD to 0, which is 1900-01-01.

    WHERE
        DATEADD(month, MonthPart-1, DATEADD(year, YearPart-1900, 0))
        > DATEADD(month, -6, GETUTCDATE())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a simple table that only contains two columns: MailingListUser -
I have a table that contains the following columns: ID int, DISTANCE float, EVENT
I have a row in an html-table that contains only images. (this happens to
I have a table that contains some meta data in an XML field. For
I have a table that contains multiple records for each day of the month,
I have a table that contains employee numbers. They need to be 5 numbers
I have a table that contains a text field, there is around 3 to
I have an SQLite table that contains a BLOB I need to do a
I have a HTML table that contains both ROWSPANs and COLSPANs. How can I
I have a table that contains multiple dropdown menus for a list of profile

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.