Jump to content

Archived

This topic is now archived and is closed to further replies.

johnnyk

DATE function

Recommended Posts

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

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote author=johnnyk link=topic=101248.msg400446#msg400446 date=1153405332]
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
[/quote]
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.
[code]
SELECT VERSION();
[/code]

Share this post


Link to post
Share on other sites
5.0.21

I had this problem before. It kept telling me there was an error in SQL syntax when there wasn't.

Share this post


Link to post
Share on other sites
Change the query to the following and post any errors. Remember to use the same script.
[code]
SELECT DAY('2006-01-02');
[/code]

Share this post


Link to post
Share on other sites

×

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.