MySQL Tutorial/Date Time Functions/NOW

Материал из SQL эксперт
Перейти к: навигация, поиск

Getting the Current Date and Time

There are three functions that you can use to get the current date and time.

NOW() gets both date and time.

CURDATE() works with only the date.

CURTIME() is for the time.



   <source lang="sql">

mysql> mysql> SELECT NOW(); +---------------------+ | NOW() | +---------------------+ | 2007-07-21 13:54:27 | +---------------------+ 1 row in set (0.00 sec)</source>


Multiple references to a function such as NOW() within a single query always produce the same result

   <source lang="sql">

mysql> This principle also applies to CURDATE(), CURTIME(), UTC_DATE(), UTC_TIME(), UTC_TIMESTAMP(), and to any of their synonyms. mysql> mysql> select now(), now(); +---------------------+---------------------+ | now() | now() | +---------------------+---------------------+ | 2007-07-22 19:46:01 | 2007-07-22 19:46:01 | +---------------------+---------------------+ 1 row in set (0.00 sec) mysql></source>


SELECT NOW(), SLEEP(2), NOW();

   <source lang="sql">

mysql> mysql> SELECT NOW(), SLEEP(2), NOW(); +---------------------+----------+---------------------+ | NOW() | SLEEP(2) | NOW() | +---------------------+----------+---------------------+ | 2007-07-22 19:46:13 | 0 | 2007-07-22 19:46:13 | +---------------------+----------+---------------------+ 1 row in set (2.02 sec) mysql></source>