Jump to content

Date format - sectioning query


sazzie

Recommended Posts

Hi,
I would like to know if there is a way to extract just the year from a date in a mysql database?

So, if I had a date in my database saved as 2006-07-05, can I extract just the 2006 in my sql query string :

ie - SELECT * FROM startDates WHERE ... this is where I want to check for 2006 ???

Please help  :)
Link to comment
Share on other sites

I tried doing the following code in php:

[code]
SELECT * FROM startDates WHERE to_date = 'stristr(date_format(to_date,'%Y'), "2006" ';
[/code]

And that did not work. You see, I am trying to not only extract the year but to match it to 2006 in the query string
so only specific entries are returned.
Link to comment
Share on other sites


Thank you very much. I can see the logic in what you have done there. :)

Just one last thing. I tried integrating you code, can you spot the syntax error :

[code]
$sql_show = "SELECT  event_ref, b.event_title, date_format(from_date, '%d/%m/%Y') as date_from, date_format(to_date, '%d/%m/%Y') as date_to, duration1, duration2, provider FROM ucl_learning_event a,  ucl_events b where a.event_title = b.id  and emp_no = '".$eNum."' and DATE_FORMAT(to_date,'".%Y."') = '2006' and da_add = '0' order by from_date";
[/code]
???
Link to comment
Share on other sites

try this, I havnt done much, just removed some of the speech marks from the query and capitalised the sql commands to make it ewasier to read
[code]
$sql_show="SELECT event_ref, b.event_title, DATE_FORMAT(from_date, '%d/%m/%Y') AS date_from, DATE_FORMAT(to_date,  '%d/%m/%Y') AS date_to, duration1, duration2, provider FROM ucl_learning_event a, ucl_events b WHERE a.event_title=b.id AND emp_no = '$eNum' AND DATE_FORMAT(to_date,'%Y') = '2006' AND da_add = 0 ORDER BY from_date";
[/code]
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.