Jump to content

display information where field has 'YYYY' in it


tjhilder

Recommended Posts

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.

Link to comment
Share on other sites

[!--quoteo(post=330075:date=Dec 23 2005, 11:36 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Dec 23 2005, 11:36 PM) 330075[/snapback][/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.

wow thanks, that works great. how does YEAR() work exactly?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.