Jump to content


Photo

DATE function


  • Please log in to reply
5 replies to this topic

#1 johnnyk

johnnyk
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 20 July 2006 - 02:22 PM

I can never seem to get the date function to work:

SELECT *, DATE_FORMAT(`datetime`, '%W, %M %D') AS `date`, DATE_FORMAT(`datetime`, '%l:%i %p') AS `time` FROM `table` WHERE `a` IS NULL AND DATE_FORMAT(`datetime`, '%Y') = '$year' AND DATE(`datetime`) >= '2006-07-20' ORDER BY `datetime` ASC

Here's the mysql_error():
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(`datetime`) >= '2006-07-20' ORDER BY `datetime` ASC' at line 1

I don't see a problem.
I'm using MySQL 5.0.21
Rediscover the Web:
<a href="http://www.spreadfir...;t=219"></a><br />

#2 fenway

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

Posted 20 July 2006 - 04:12 PM

Well, there's nothing wrong with that syntax at all... I assume that your substitution for $year is correct? Post the query after the interpolation step.  BTW, you really shouldn't name your column "datetime".
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 johnnyk

johnnyk
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 24 July 2006 - 11:07 PM

This isn't the first time I've gotten "SQL error" things when the SQL was fine. It happened before and I don't know why. Interpolation? I am confused what you mean.

And what's wrong with naming it datetime? I never name my columns according to their type, except for datetime. The php never uses $row['datetime']. I just split the datetime up to date and time using SQL. What else would I name it, and why would it matter?
Rediscover the Web:
<a href="http://www.spreadfir...;t=219"></a><br />

#4 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 25 July 2006 - 02:28 AM

I can never seem to get the date function to work:

SELECT *, DATE_FORMAT(`datetime`, '%W, %M %D') AS `date`, DATE_FORMAT(`datetime`, '%l:%i %p') AS `time` FROM `table` WHERE `a` IS NULL AND DATE_FORMAT(`datetime`, '%Y') = '$year' AND DATE(`datetime`) >= '2006-07-20' ORDER BY `datetime` ASC

Here's the mysql_error():
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(`datetime`) >= '2006-07-20' ORDER BY `datetime` ASC' at line 1

I don't see a problem.
I'm using MySQL 5.0.21

If that's the exact query you ran then I can only assume that your version of MYSQL is actually lower than 4.1. The syntax error you posted would be caused by the fact that the DATE function only became available in version 4.1.1 if that is the case.

Run the query below using the same script to verify the version.
SELECT VERSION();


#5 johnnyk

johnnyk
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 25 July 2006 - 04:41 AM

5.0.21

I had this problem before. It kept telling me there was an error in SQL syntax when there wasn't.
Rediscover the Web:
<a href="http://www.spreadfir...;t=219"></a><br />

#6 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 25 July 2006 - 05:50 AM

Change the query to the following and post any errors. Remember to use the same script.
SELECT DAY('2006-01-02');





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users