How to get past dates from previous years?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Neal Moriarty
    New Member
    • Dec 2010
    • 2

    #1

    How to get past dates from previous years?

    Here's my SQL so far...

    Code:
    SELECT Format(Day([Actual Date]=Day(Date())) And Month([Actual Date]=Month(Date()))) AS "Past Date"
    FROM [Gas Data]
    ORDER BY Format([Actual Date],"mm/dd/yyyy") DESC , [Gas Data].[MCF sendout] DESC , [Gas Data].[Degree Days] DESC;
    What I'm trying to do is get the same past date from the last three years for a report. For instance, for today I would need 12/3/2009, 12/3/2008 and 12/3/2007. There's a field called "Actual Date" which I'm hoping I can parse down to return the day and month for my report, but so far no success...any help would be greatly appreciated!!!

    Many thanks!
    Neal
    Last edited by NeoPa; Dec 5 '10, 12:30 AM. Reason: Added CODE tags
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi. There are some problems with your SQL, in that you've mixed up what you are trying to select with how you want to select it.

    You want to select all rows in which the [Actual Date] field matches the current day and month. Try:

    Code:
    SELECT   [Actual Date] AS [Past Date]
    FROM     [Gas Data]
    WHERE    Month([Actual Date]) = Month(Date()) 
    AND      Day  ([Actual Date]) = Day(Date())
    ORDER BY [Actual Date] DESC;
    Be aware if you use FORMAT that it returns text values, and you cannot order dates formatted as text unless you return them with the year reversed: yyyy/mm/dd for example. There is, however, no need to use format for what you want to do.

    -Stewart

    PS if there are no rows in table [Gas Data] with [Actual Dates] matching the current day and month then the query will not return any rows at all. Depending on what you do with this query for your report you may not find it of practical use in itself.
    Last edited by Stewart Ross; Dec 4 '10, 08:24 AM. Reason: Added PS

    Comment

    • Neal Moriarty
      New Member
      • Dec 2010
      • 2

      #3
      Awesome Stewart! That did it, works perfectly now. I knew what I wanted it to do, just could bend the SQL to my will to do so. Thanks a million!

      Neal

      Comment

      Working...