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 6031769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:14:03+00:00 2026-05-23T05:14:03+00:00

I have a column called [date] and the data in that column is something

  • 0

I have a column called [date] and the data in that column is something like this:

[date]

2011/01
2011/02
2011/03
2011/04
2011/05
2011/06
2011/07
2011/08
2011/09
2011/10

My full scenario is that I have created a column named [date] and I am dumping the data to that column from another column called [fiscal_month] (both are from same table).

I have to retrieve the latest 9 months data from that [date] column and I have written this code:

SELECT TOP 9 REPLACE (P.[Fiscal Month],'M','/') as [date],
      P.[Fiscal Month] AS [Fiscal Month] FROM [OR].[Report].[SD_P] P 
WHERE [date] <= CONVERT(char(7), GETDATE(), 111) ORDER BY [date] DESC 

The [fiscal_month] column is VARCHAR(100).

The data in the [fiscal_month] column is something like this:

2011M01,2011M02, 2011M03, 2011M04, 2011M05

etc

Update:

I altered the [date] column and I have assigned the datetime datatype for that column and then I select into it with the replacement of ‘M’ from the other column with ‘/’ to get the 9 months latest data..

  • 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-23T05:14:04+00:00Added an answer on May 23, 2026 at 5:14 am

    I’m assuming that the date column is actually stored as text. If that’s the case, the easiest solution is to convert it to a datetime and then do your comparison:

    Select ...
    From MyTable
    Where Cast(Replace([Date],'/','') + '01' As DateTime)
        > DateAdd(m, -9
             , DateAdd(d
                 , -Day(GetDate())
                 , DateAdd(d, DateDiff(d, 0, GetDate() ), 0)))
    

    The following snippet is used to strip the time portion from today’s date:

    DateAdd( d, DateDiff(d, 0, GetDate() ), 0 )
    

    Update

    Given your addition, you only need a small change the query above:

    Select ...
    From MyTable
    Where Cast(Replace([Date],'M','') + '01' As DateTime)
        > DateAdd(m, -9
             , DateAdd(d
                 , -Day(GetDate())
                 , DateAdd(d, DateDiff(d, 0, GetDate() ), 0)))
    

    Update

    Given that you have now converted your date column to datetime, you can simplify your query:

    Select ...
    From MyTable
    Where [Date] > DateAdd(m, -9, DateAdd(d
                                   , -Day(GetDate())
                                   , DateAdd(d, DateDiff(d, 0, GetDate() ), 0)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame that looks like this: site date var dil 1
In mysql database i have this column called: Name: Date Type: datetime I have
If I have table with a Date column called myDate , with values like
We have a custom list that has a column called Connection Date and need
I have a date and time column in my mysql table called start_date and
I have the column DateTimeExpired, and I would like to create another column called
I have created a table called myTable with a column called CURRENT_DATE and it's
I have a column called DayShift in a table which is of Yes/No data
I have a column of data in SQL that is currently in the datetime
I have a column called post_tags where there is sometimes one tag entry and

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.