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 > current year

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-24-11, 07:05
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
current year

How do I express the current year in the following calculation? I've readed masses of notes but can't find this answer.

I have to find horses over 15 years old. horse_born is the column containing the horse's year of birth:

SELECT horse_id, horse_name
FROM horse
WHERE "expression for current year" - horse_born >15
Reply With Quote
  #2 (permalink)  
Old 03-24-11, 08:39
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
After extensive searching I found the following page.
The year function looked particularly relevant.
__________________
Mike
Reply With Quote
  #3 (permalink)  
Old 03-24-11, 09:28
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
To add to Mike's suggestion try using curdate() or now() to get today's date or date/time.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #4 (permalink)  
Old 03-24-11, 12:20
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by it-iss.com View Post
... try using curdate() or now() to get today's date or date/time.
please, don't use those

use these instead -- CURRENT_DATE, CURRENT_TIMESTAMP

they are totally equivalent, ~and~ they will also work in other database systems, thus making your code portable and not dependent on proprietary mysql syntax

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-24-11, 12:22
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by melgra70 View Post
WHERE "expression for current year" - horse_born >15
don't do a calculation involving the column value, the database engine won't be able to optimize the query

instead, put the column value on one side of the comparison operator, and do the calculation on the other side --

WHERE horse_born < YEAR(CURRENT_DATE) - INTERVAL 15 YEAR

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 03-24-11, 12:42
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I think I preferred it when we provided NZDF solutions to homework questions rather than high efficiency multi-platform solutions. It was also far more fun.
__________________
Mike
Reply With Quote
  #7 (permalink)  
Old 03-24-11, 16:16
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by mike_bike_kite View Post
I think I preferred it when we provided NZDF solutions to homework questions
i believe that i did

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 03-24-11, 16:35
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I obviously wasn't paying enough attention in class then
__________________
Mike
Reply With Quote
  #9 (permalink)  
Old 03-24-11, 16:39
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
so i guess this was a good nzdf then?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 03-24-11, 17:24
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
oh definitely - keep up the good work
__________________
Mike
Reply With Quote
  #11 (permalink)  
Old 03-24-11, 19:40
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
nzdf - what does this mean? I googled it and found New Zealand Defense Force and New Zealand Drillers Federation - though I don't think either apply here
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #12 (permalink)  
Old 03-24-11, 20:14
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
NZDF is a term which was first coined here in these very forums by, ahem, yours truly

it stands for Non-Zero Deviosity Factor, and refers to the practice of responding to certain requests for homework assistance with an answer that is devious to some extent

obviously, a student struggling with a homework problem, who makes a game attempt, and shows his work, and discusses what he's tried and what concepts he's having trouble with, is going to receive answers largely devoid of any deviosity

the idea is that the larger the deviosity, the more likely it is that the student will get into trouble if he hands in the answer he obtained here

and it's particularly effective if the answer actually happens to work correctly, but in a way so devious that the student doesn't twig onto the ruse but the teacher surely will

can you see what the NZDF was in this thread?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 03-25-11, 03:31
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
LOL - of course as a student once I was more intent on getting the solution working, less about performance and cross platform compatibility. I like it - NZDF!! I will have to use this too!!
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #14 (permalink)  
Old 03-25-11, 05:00
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I wonder whether we should have a monthly award for the best response. The only danger with NZDF is that some future associate might look for your name on the web and come up with the code and just think WTF?
__________________
Mike
Reply With Quote
  #15 (permalink)  
Old 03-25-11, 12:51
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by mike_bike_kite View Post
I wonder whether we should have a monthly award for the best response.
pat would win it in a cakewalk every month

i've never seen anybody come even close to his level of deviosity

__________________
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