Jump to content

SELECT substr("time",4) AS year


Donovan

Recommended Posts

Is this possible?  I need to be able to filter the year in a select clause.

 

I have a value in a field called time...

 

2008-01-17 13:02:07

 

I need to capture the year, which is the first 4 characters.  I will do this by a list box and filter what questions are displayed.

 

Here is a snippet:

Opentable();
echo"<tr><center>Use drop down box to view prior years questions.</center></tr>";
echo"<tr><center>Select Year <select name='year' size=\"1\" onchange=\"reload(this.form)\">";
echo"<option value=\"2007\" selected>2007</option>"
."<option value=\"2008\">2008</option>"
."<option value=\"2009\">2009</option>"
."<option value=\"2010\">2010</option>"	
."</select></center></tr>"
."</form>";	
Closetable();

$result = $db->sql_query("SELECT substr("time",4) AS year, * FROM comments cm 
JOIN course cs ON (cm.course_id = cs.course_id) 
JOIN reply r ON (cm.cid = r.cid) 
WHERE cm.course_id = '$course_id' 
ORDER BY time DESC");	
while ($row = $db->sql_fetchrow($result))	{	

Link to comment
Share on other sites

It's DATETIME. 

 

I'm thinking I need a separate SELECT statement to get the value of the listbox.

 

Opentable();
echo"<tr><center>Use drop down box to view prior years questions.</center></tr>";
echo"<tr><center>Select Year <select name=\"year\" size=\"1\" onchange=\"document.forms[0].submit();\">";
echo"<option value=\"2007\">2007</option>"
."<option value=\"2008\"selected>2008</option>"
."<option value=\"2009\">2009</option>"
."<option value=\"2010\">2010</option>"	
."</select></center></tr>"
."</form>";	
Closetable();

$result = $db->sql_query("SELECT YEAR(cm.time) AS qyear, * FROM comments cm 
JOIN course cs ON (cm.course_id = cs.course_id) 
JOIN reply r ON (cm.cid = r.cid) 
WHERE cm.course_id = '$course_id' 
AND qyear = '$year'
ORDER BY cm.time DESC");

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.