Jump to content


Photo

display information where field has 'YYYY' in it


  • Please log in to reply
3 replies to this topic

#1 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 23 December 2005 - 10:14 PM

I have a table called gallery and in that table is a field called date_sort, in that is information like YYYY-MM-DD.

is it possible to have the query that looks like this..

$view_2005 = "SELECT * FROM gallery WHERE date_sort='2005' ORDER BY date_sort DESC";

changed so that it only displays the results that start with 2005-MM-DD ? how would I make this possible?

thanks in advance.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 23 December 2005 - 11:36 PM

Yes -- use the YEAR() function:

$view_2005 = "SELECT * FROM gallery WHERE YEAR(date_sort)='2005' ORDER BY date_sort DESC";


Hope that helps.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 23 December 2005 - 11:44 PM

[!--quoteo(post=330075:date=Dec 23 2005, 11:36 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Dec 23 2005, 11:36 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Yes -- use the YEAR() function:

$view_2005 = "SELECT * FROM gallery WHERE YEAR(date_sort)='2005' ORDER BY date_sort DESC";


Hope that helps.
[/quote]
wow thanks, that works great. how does YEAR() work exactly?

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 24 December 2005 - 12:08 AM

Most of these are shortcut functions to the DATE_FORMAT() function, which allows you to retrieve just about any piece of the a date value that you can imagine. So, YEAR() is equivalent to calling DATE_FORMAT(date_field, '%Y'). Personally, I like to use the latter for flexibility and version back compatibility, but using YEAR() can make the code more readable.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users