Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2004
    Posts
    3

    Unanswered: SQL question with dates

    Hi, I have got a table where the month and year are separated into two different columns. Example:

    Month Year ID
    3 2005 123
    2 2005 123
    1 2005 123
    12 2004 123
    11 2004 123
    10 2004 123
    9 2004 123
    8 2004 123
    7 2004 123
    6 2004 123
    5 2004 123
    4 2004 123
    .
    .
    .
    and so on...

    I am passing in the last entry for month and year (3, 2005) in my WHERE clause and I want to retrieve all the entries within the past year.. (so returns the 12 records above from 4/2004 to 3/2005).

    How can I do this? Note that the fields in that table are not of DATE type.

    Thanks!

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    If you are passing in the month and year of the last row that you want, and you want it and all preceeding rows, you need to use logic like:
    Code:
    WHERE  year < 2005 OR (2005 = year AND month <= 3)
    If you meant something different, can you explain whatever I'm missing?

    -PatP

  3. #3
    Join Date
    Apr 2004
    Posts
    3
    Thanks for your reply.

    The problem is I don't want all the preceding data. I just want 12 months before the date that I specify.

    So in the case where you had "year < 2005", then that would pick up all my old records, whereas I only want the records from April 2004 to May 2005.

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Ah-ha! That makes the request a lot clearer. Now I'd suggest:
    Code:
    WHERE (2005 = year AND month <= 3) OR (2004 = year AND 3 < month)
    This should get the range that you want.

    -PatP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •