If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL question with dates

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-29-04, 14:20
forsberg forsberg is offline
Registered User
 
Join Date: Apr 2004
Posts: 3
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!
Reply With Quote
  #2 (permalink)  
Old 04-29-04, 14:53
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
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
Reply With Quote
  #3 (permalink)  
Old 04-29-04, 17:45
forsberg forsberg is offline
Registered User
 
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.
Reply With Quote
  #4 (permalink)  
Old 04-29-04, 23:22
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On