Mysql date and time-related functions

Source: Internet
Author: User

Directory:

    1. Common date functions
    2. Time-plus-decrement function
    3. Date_forma function

1. Common date functions

Now () Current_timestamp () sysdate ()

    • Example one:

  

You can see that three functions are used to get the current system time.

    • Example two:

  

In the operation, the sleep function was added, waiting for 2 seconds, then found that the Sysdate function and the other two functions to get the value of different, sysdate function to get the value of 2 seconds faster. So three functions are slightly different, the difference is as follows:

    • Current_timestamp and now are synonyms, and the two are the same.
    • The Sysdate function returns the time it takes to execute to the current function, and now returns the time when the SQL statement was executed.

2. Time-plus-decrement function

Date_add (date, interval expr unit) date_sub (date, interval expr unit)
    • Example one:

  

The value of expr can be positive or negative, so you can use the Date_add function to do the work of the Date_sub function, for example:
  
  

    • Example two:

  

In the above example, the unit parameter uses day, hour, in fact, you can also use microsecond, second, minute, week, month, year and other types.

3. Date_format function

    • Example one: 
Select ' %y%m%d '  as DateTime;

Operation Result:

  

  

Mysql date and time-related functions

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.