MySQL time and date functions

Source: Internet
Author: User
In database applications, date and time operations are very common. MySQL comes with many useful functions that can help us easily implement certain requirements. Next we will introduce the related MySQL date and practice functions. Obtain the current date: mysqlSELECTcurdate (); + ------------ + | curdate () | + ------------ + | 2010-1

In database applications, date and time operations are very common. MySQL comes with many useful functions that can help us easily implement certain requirements. Next we will introduce the related MySQL date and practice functions. Obtain the current date: mysql> SELECT curdate (); + ------------ + | curdate () | + ------------ + | 2010-1

In database applications, date and time operations are very common. MySQL comes with many useful functions that can help us easily implement certain requirements. Next we will introduce the related MySQL date and practice functions.

Get current date:

mysql> SELECT curdate();+------------+| curdate()  |+------------+| 2010-11-16 |+------------+1 row in set
mysql> SELECT CURRENT_DATE();+----------------+| CURRENT_DATE() |+----------------+| 2010-11-16     |+----------------+1 row in set

Get current time:

mysql> SELECT curtime();+-----------+| curtime() |+-----------+| 12:48:40  |+-----------+1 row in set
mysql> SELECT CURRENT_TIME();+----------------+| CURRENT_TIME() |+----------------+| 12:49:00       |+----------------+1 row in set

Get the current time and date:

mysql> SELECT now();+---------------------+| now()               |+---------------------+| 2010-11-16 12:50:39 |+---------------------+1 row in set
mysql> SELECT CURRENT_TIMESTAMP();+---------------------+| CURRENT_TIMESTAMP() |+---------------------+| 2010-11-16 12:51:01 |+---------------------+1 row in set

Note: In a single query, multiple accesses to functions such as NOW () always get the same result.

Date increase/decrease:

DATE_ADD(date,INTERVAL expr type)DATE_SUB(date,INTERVAL expr type)

These functions perform date operations.

  • Date is a DATETIME or DATE value, used to specify the start time.
  • Expr is an expression used to specify the time interval value added or subtracted from the start date. Expr is a string. For the time interval of negative values, it can start with.
  • Type indicates the expression interpretation method.
  • The keywords INTERVA and type are case insensitive.

MySQL allows punctuation separators in any expr format. The recommended Delimiter is displayed in the table. If the date parameter is a DATE value, and your calculation only includes the YEAR, MONTH, and DAY sections (that is, there is no time section), the result is a DATE value. Otherwise, the result is a DATETIME value.

After the specified date is 1 second:

mysql> SELECT DATE_ADD('2010-12-31 23:59:59', INTERVAL 1 SECOND);+----------------------------------------------------+| DATE_ADD('2010-12-31 23:59:59', INTERVAL 1 SECOND) |+----------------------------------------------------+| 2011-01-01 00:00:00                                |+----------------------------------------------------+1 row in set

One day after the specified date:

mysql> SELECT DATE_ADD('2010-12-31 23:59:59', INTERVAL 1 DAY);+-------------------------------------------------+| DATE_ADD('2010-12-31 23:59:59', INTERVAL 1 DAY) |+-------------------------------------------------+| 2011-01-01 23:59:59                             |+-------------------------------------------------+1 row in set

Specified date minus 10 hours:

mysql> SELECT DATE_ADD('2011-01-01 00:00:00', INTERVAL '-1 10' DAY_HOUR);+------------------------------------------------------------+| DATE_ADD('2011-01-01 00:00:00', INTERVAL '-1 10' DAY_HOUR) |+------------------------------------------------------------+| 2010-12-30 14:00:00                                        |+------------------------------------------------------------+1 row in set

One month before the specified date:

mysql> SELECT DATE_SUB('2011-01-02', INTERVAL 31 DAY);+-----------------------------------------+| DATE_SUB('2011-01-02', INTERVAL 31 DAY) |+-----------------------------------------+| 2010-12-02                              |+-----------------------------------------+1 row in set

The day before the specified date:

mysql> SELECT date_add('2011-01-01', INTERVAL -1 DAY);+-----------------------------------------+| date_add('2011-01-01', INTERVAL -1 DAY) |+-----------------------------------------+| 2010-12-31                              |+-----------------------------------------+1 row in set

Convert the date and time to the specified format:

DATE_FORMAT (date, format)

Format the date value according to the format string. All other characters are copied to the result without explanation. Note that the '%' character must be before the format specified. The range of month and date specifiers starts from scratch because MySQL allows storage of incomplete dates such as '2017-00-00.

mysql> SELECT DATE_FORMAT( FROM_UNIXTIME( 1290996580 ), '%Y-%m-%d %H:%i:%s' );+-----------------------------------------------------------------+| DATE_FORMAT( FROM_UNIXTIME( 1290996580 ), '%Y-%m-%d %H:%i:%s' ) |+-----------------------------------------------------------------+| 2010-11-29 10:09:40                                             |+-----------------------------------------------------------------+1 row in set
mysql> SELECT DATE_FORMAT( FROM_UNIXTIME( 1290996580 ) , '%Y-%m-%d' );+---------------------------------------------------------+| DATE_FORMAT( FROM_UNIXTIME( 1290996580 ) , '%Y-%m-%d' ) |+---------------------------------------------------------+| 2010-11-29                                              |+---------------------------------------------------------+1 row in set

The following two functions

FROM_UNIXTIME(unix_timestamp) ,FROM_UNIXTIME(unix_timestamp,format)

Returns the unix_timestamp Parameter Representation of the 'yyyy-MM-DD HH: MM: ss' or YYYYMMDDHHMMSS format value, depending on whether the function is used in a string or a numeric context.

If the format is provided, the format of the result is determined by the format string. Format can contain the same specifiers as in the input item list of the DATE_FORMAT () function.

mysql> SELECT FROM_UNIXTIME(875996580);+--------------------------+| FROM_UNIXTIME(875996580) |+--------------------------+| 1997-10-05 04:23:00      |+--------------------------+1 row in set
mysql> SELECT FROM_UNIXTIME(875996580) + 0;+------------------------------+| FROM_UNIXTIME(875996580) + 0 |+------------------------------+|        19971005042300.000000 |+------------------------------+1 row in set

MySql unix time is converted to the applicable time format of SQLServer:

mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y-%m-%d %H:%i:%s');+------------------------------------------------------+| FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y-%m-%d %H:%i:%s') |+------------------------------------------------------+| 2010-11-16 19:12:11                                  |+------------------------------------------------------+1 row in set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.