MySQL time plus and minus function

Source: Internet
Author: User

Let's look at an example:

Select Now (), now () +0;

You can see that the now () function can return a time or return a number to see how you use it. If you increase or decrease relative to the current time, you cannot add or subtract a number directly but you need a specific function. such as Date_add () or date_sub (), the former represents an increase, the latter represents a decrease. Its specific use methods are Date_add (Date,interval expr uint) and date_sub (Date,interval expr uint). Examples are as follows:

Select Now () as now,
Date_add (now (), Interval 1 day) as tomorrow,
Date_sub (now (), Interval 1 day) as yesterday;

Where expr can be positive or negative, you can use Date_add () to complete the work of Date_sub (). Examples are as follows:

There is one more question, if it is a leap month, how are these two functions handled?? The default behavior for MySQL is this: if the target year is a leap month, the returned date is February 29, and vice versa is February 28. Examples are as follows:

In the example above, using the Year,day data type can also use data types such as microsecond, second, minute, hour, week, month, and so on.

MySQL time plus and minus function

Related Article

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.