MySql intercepts the DateTime field's date value, mysqldatetime

Source: Internet
Author: User

MySql intercepts the DateTime field's date value, mysqldatetime

Use DATE_FORMAT to format the Date Field

SELECT DATE_FORMAT(crt_time,'%Y-%m-%d')  FROM ad_n_advertise_t


In mysql, how does one obtain the year and month of a date field? For example, if the value of the signDate field is, I only need 2013-12, an SQL statement

Mysql has a date function date_format
SELECT date_format (signDate, '% Y-% m') AS MY_YM FROM tablename WHERE 1;

How to add a fixed time for the datetime field in mysql

PERIOD_DIFF (P1, P2)
Returns the number of months between period P1 and P2. P1 and P2 should be in the format of YYMM or YYYYMM. Note that the period parameters P1 and P2 are not date values.
Mysql> select PERIOD_DIFF (9802,199703 );
-> 11

DATE_ADD (date, INTERVAL expr type)
 
DATE_SUB (date, INTERVAL expr type)
 
ADDDATE (date, INTERVAL expr type)
 
SUBDATE (date, INTERVAL expr type)
These functions perform date operations. For MySQL 3.22, they are new. ADDDATE () and SUBDATE () are synonyms of DATE_ADD () and DATE_SUB.
In MySQL 3.23, you can use + and-instead of DATE_ADD () and DATE_SUB (). (See the example) date is a specified start date.
DATETIME or DATE value. expr is an expression that specifies the value to be added to the start DATE or the value to be subtracted from the start DATE. expr is a string.
A "-" indicates the negative interval. Type is a keyword that specifies how the expression should be interpreted. EXTRACT (type FROM date) function FROM date
Returns the "type" interval. The following table shows how the type and expr parameters are associated: The type value indicates the expected expr format.
SECOND SECONDS
MINUTE MINUTES
HOUR time HOURS
DAY DAYS
MONTH-MONTH MONTHS
YEAR YEARS
MINUTE_SECOND MINUTES and SECONDS "MINUTES: SECONDS"
HOUR_MINUTE hour and minute "HOURS: MINUTES"
DAY_HOUR and hour "days hours"
YEAR_MONTH and month "YEARS-MONTHS"
HOUR_SECOND hour, minute, "HOURS: MINUTES: SECONDS"
DAY_MINUTE day, hour, minute "days hours: MINUTES"
DAY_SECOND day, hour, minute, second "days hours: MINUTES: SECONDS"

MySQL allows any punctuation Separator in the expr format. It indicates that the recommended Delimiter is displayed. If the date parameter is a DATE value and your calculation is only
Contains YEAR, MONTH, and DAY (that is, there is no time), and the result is a DATE value. Otherwise, the result is a DATETIME value.

Mysql> SELECT "23:59:59" + INTERVAL 1 SECOND;
-> 00:00:00
Mysql> select interval 1 DAY + "1997-12-31 ";
-> 1998-01-01
Mysql> SELECT "1998-01-01"-INTERVAL 1 SECOND;
-> 1997-12-31 23:59:59
Mysql> SELECT DATE_ADD ("19 ...... the remaining full text>

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.