By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

What is the NOW() function in a MySQL DateTime field query?

P: 14
I have a monitoring script which I am trying to better understand. One of the queries is built as follows:
Expand|Select|Wrap|Line Numbers
  1. # delete any rows that expired more than a year ago
  2. my $rows = $dbh->do("DELETE FROM table1 WHERE last_update < NOW() - INTERVAL 2 YEAR ");
  3.  
What is the NOW() function, and what Perl module does it come in? Is there more functions?
May 31 '08 #1
Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,058
Hi.

NOW() is a MySQL function, not a Perl function.
It returns a DateTime value for the current date and time.

Check out Functions and Operators in the MySQL reference manual.
May 31 '08 #2

P: 14
Can you tell me why this is an incorrect statement?
Expand|Select|Wrap|Line Numbers
  1. SELECT count( )
  2. FROM `table1`
  3. WHERE origin_type = 'MANUAL'
  4. AND DATE( 'create_time' ) = CURDATE( )
  5.  
MySQL said: Documentation
#1064 - 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 ') FROM `table1` WHERE origin_type = 'MANUAL' and DATE('create_time') = CURD' at line 1

create_time is a DateTime field. I want to be able to select the records which match the current date.
Jun 1 '08 #3

Atli
Expert 5K+
P: 5,058
You need to specify a column for the count() function.
Like, for example:
Expand|Select|Wrap|Line Numbers
  1. SELECT count(*) FROM myTable;
  2.  
The wild-card char will always work, no matter how you table is structured.

Also, if the 'create_time' part of your DATE() function is a column name, you are going to want to enclose it in back-ticks(`) rather than single-quotes(').

Only strings are quoted. The back-ticks are used for column, table and database names that contain characters that would create problems for the parser, like spaces.
Like:
Expand|Select|Wrap|Line Numbers
  1. SELECT count(*) FROM `My Table`;
  2.  
P.S.
Please put all code examples inside [code] tags.
Thanks.
Jun 1 '08 #4

P: 14
You need to specify a column for the count() function.
Like, for example:
Expand|Select|Wrap|Line Numbers
  1. SELECT count(*) FROM myTable;
  2.  
The wild-card char will always work, no matter how you table is structured.
Ouch, that was what I missed. Thank you.
Also, if the 'create_time' part of your DATE() function is a column name, you are going to want to enclose it in back-ticks(`) rather than single-quotes(').

Only strings are quoted. The back-ticks are used for column, table and database names that contain characters that would create problems for the parser, like spaces.
Like:
Expand|Select|Wrap|Line Numbers
  1. SELECT count(*) FROM `My Table`;
  2.  
P.S.
Please put all code examples inside [code] tags.
Thanks.
Thank you for the explanation, I was wondering about the meaning(s) for the tick, quote, and double quotes.
Jun 3 '08 #5

Atli
Expert 5K+
P: 5,058
Glad I could help :)
Jun 3 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.