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 > Database Server Software > MySQL > This year only

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-17-09, 14:52
oldnickj oldnickj is offline
Registered User
 
Join Date: Jan 2009
Posts: 103
This year only

This filter returns every entry from now into the future, how do I limit to this year only (AND datestart_eve < (next year)

WHERE race_region.id_reg=3 AND datestart_eve > NOW()

Nick
Reply With Quote
  #2 (permalink)  
Old 04-17-09, 22:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
for the lower bound, if today is january 1, you want everything that is greater than or equal to today, i.e. greater than today - 0 days

if today is january 2, you want everything that is greater than or equal to yesterday, i.e. greater than today - 1 days

thus the general pattern for the lower bound is today minus the number of days that we are into this year, plus 1 day, i.e. DAYOFYEAR() + 1

using that formula gives us january 1st of this year, so if we add 1 year plus, we have the upper bound
Code:
WHERE ... 
  AND datestart_eve >= CURRENT_DATE - INTERVAL DAYOFYEAR(CURRENT_DATE) + 1 DAY
  AND datestart_eve  < CURRENT_DATE - INTERVAL DAYOFYEAR(CURRENT_DATE) + 1 DAY + INTERVAL 1 year
make sense?

note: "greater than or equal to" for the lower bound, but "less than" for the upper bound

this formula works whether the column in question (datestart_eve in this case) contains dates or datetime values

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 04-17-09 at 22:41.
Reply With Quote
  #3 (permalink)  
Old 04-18-09, 11:53
oldnickj oldnickj is offline
Registered User
 
Join Date: Jan 2009
Posts: 103
Current year only.

Doesn't this return records for the next year rather than only record from this year. I suppose I formed my question incorrectly. We ant the record for this year only. 2009 this year 2010 next year.

Nick
Reply With Quote
  #4 (permalink)  
Old 04-18-09, 14:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by oldnickj
Doesn't this return records for the next year rather than only record from this year.
no, i don't think so
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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