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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:26:02+00:00 2026-06-10T03:26:02+00:00

If today is say 15th August 2012 then the query should return the following

  • 0

If today is say 15th August 2012 then the query should return the following

15/01/2011,
15/02/2011,
...
...
15/07/2012
15/08/2012

If today is 31st August 2012 then the query would return

31/01/2011,
28/02/2011,  <<<<this is the nearest date
...
...
31/07/2012
31/08/2012

We have a vw_DimDate in our Warehouse which should help

edit
It contains the following fields

enter image description here

Currently I’m using the following but it seems rather convoluted! …

DECLARE @Dt DATETIME = '31 JUL 2012'--GETDATE()

;WITH DateSet_cte(DayMarker)
        AS
        (
        SELECT DayMarker
        FROM WHData.dbo.vw_DimDate
        WHERE 
                DayMarker >= CONVERT(DATETIME,CONVERT(CHAR(4),DATEADD(YEAR,-1,@Dt),112) + '0101') AND
                DayMarker <=@Dt
        )
, MaxDate_cte(MaxDate)
        AS
        (
        SELECT [MaxDate] = MAX(DayMarker)
        FROM DateSet_cte 
        )       
SELECT
            [Mth] = CONVERT(DATETIME,CONVERT(CHAR(6),a.DayMarker,112) + '01')
            , MAX(a.DayMarker) [EquivDate]
FROM DateSet_cte a
WHERE DAY(a.DayMarker) <= (SELECT DAY([MaxDate]) FROM MaxDate_cte)
GROUP BY CONVERT(DATETIME,CONVERT(CHAR(6),a.DayMarker,112) + '01')
  • 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-10T03:26:04+00:00Added an answer on June 10, 2026 at 3:26 am
    ;with Numbers as (
        select distinct number from master..spt_values where number between 0 and 23
    ), Today as (
        select CONVERT(date,CURRENT_TIMESTAMP) as d
    )
    select
        DATEADD(month,-number,d)
    from
        Numbers,Today
    where DATEPART(year,DATEADD(month,-number,d)) >= DATEPART(year,d) - 1
    

    Seems odd to want a variable number of returned values based on how far through the year we are, but that’s what I’ve implemented.

    When you use DATEADD to add months to a value, then it automatically adjusts the day number if it would have produced an out of range date (e.g. 31st February), such that it’s the last day of the month. Or, as the documentation puts it:

    If datepart is month and the date month has more days than the return month and the date day does not exist in the return month, the last day of the return month is returned.

    Of course, if you already have a numbers table in your database, you can eliminate the first CTE. You mentioned that you “have a vw_DimDate in our Warehouse which should help”, but since I have no idea on what that (presumably, a) view contains, it wasn’t any help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following: NSDate *today = [NSDate date]; How do I just
Let's say I had a string like this: string source = Today is friday!
I have a variable foo that contains a time, lets say 4pm today, but
Today is the first time I ever expose to LINQ so to say I
An interesting question arose today. Let's say I have a .NET object that implements
Today Microsoft released Windows 8 Release Preview and visual studio 2012 rc. I'm a
Let say i have a text file with today is today but tomorrow is
Let's say today I work from 09:00 to 18:00, I have 3 appointments: 10:00
Let's say I made a commit today, and tomorrow another Developer made a commit,
Say I want to display a graph of today's stock market. I gather all

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.