MySQL Time Functions

Source: Internet
Author: User
Tags modifiers
Returns the week index of date (0 = Monday, 1 = Tuesday ,...... 6 = Sunday ). Mysql> select weekday ('2017-10-04 22:23:00 ');-> 5 mysql> select weekday ('2017-11-05');-> 2 dayofmonth (date) returns the date of a month in the range of 1 to 31. Mysql> select dayofmonth ('1970-02-03 ');-> 3 dayofyear (date) returns the number of days in a year, within the range of 1 to 1998. Mysql> select dayofyear ('1970-02-03 ');-> 34 month (date) returns the month of date, ranging from 1 to 12. Mysql> select month ('2014-02-03 ');-> 2 dayname (date) returns the name of the week of date. Mysql> select dayname ("");-> 'thursday' monthname (date) returns the name of the Month of date. Mysql> select monthname ("1998-02-05");-> 'february 'quarter (date) returns the quarter of the year of date, ranging from 1 to 4. Mysql> select quarter ('98-04-01 ');-> 2 week (date) Week (date, first) where Sunday is the first day of a week, there is a single parameter, returns the week number of date, ranging from 0 to 52. Two Parameter formats: Week () allows you to specify whether the week starts on Sunday or Monday. If the second parameter is 0, the week starts from Sunday, and if the second parameter is 1, it starts from Monday. Mysql> select Week ('2017-02-20 ');-> 7 mysql> select Week ('2017-02-20', 0 ); -> 7 mysql> select Week ('2014-02-20 ', 1);-> 8 year (date) returns the year of date, ranging from 1998 to 1000. Mysql> Select Year ('98-02-03 ');-> 1998 hour (time) returns the hour of time, ranging from 0 to 23. Mysql> select hour ('10: 05: 03');-> 10 minute (time) returns the minute of time, ranging from 0 to 59. Mysql> select minute ('98-02-03 10:05:03 ');-> 5 second (time) returns the time in seconds, ranging from 0 to 59. Mysql> select second ('10: 05: 03');-> 3 period_add (p, n) add n months to phase P (in the format of yymm or yyyymm ). Return Value in the format of yyyymm. Note that the phase parameter P is not a date value. Mysql> select period_add (9801,2);-> 199803 period_diff (P1, P2) returns the number of months between the 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 datetime or date value that specifies the start date. expr is an expression that specifies the interval value added to the start date or subtracted from the start date. expr is a string; it can start with a "-" to indicate the negative interval. Type is a keyword that specifies how the expression should be interpreted. The extract (type from date) function returns the "type" interval from the date. The following table shows how the type and expr parameters are associated: the value of type indicates the expected expr format: Second second seconds minute minutes hour time hours day days month year years minute_second minute and second "minutes: seconds "hour_minute hour and minute" hours: Minutes "day_hour day 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 "mysq L any punctuation separator is allowed in the expr format. It indicates that the recommended Delimiter is displayed. If the date parameter is a date value and your calculation only contains 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. Mysql> select "23:59:59" + interval 1 second;-> 00:00:00 mysql> select interval 1 day + "1997-12-31 "; -> mysql> select ""-interval 1 second;-> 23:59:59 mysql> select date_add ("23:59:59", interval 1 second ); -> 00:00:00 mysql> select date_add ("23:59:59", interval 1 day);-> 23:59:59 mysql> select d Ate_add ("23:59:59", interval "00:01:00" minute_second);-> 00:00:00 mysql> select date_sub ("", interval "1" day_second ); -> 22:58:59 mysql> select date_add ("00:00:00", interval "-1 10" day_hour);-> 14:00:00 mysql> select date_sub ", interval 31 day);-> 1997-12-02 mysql> select extract (year from ");-> 1999 mysql> select extract (year_month from "01:02:03");-> 199907 mysql> select extract (day_minute from "01:02:03 "); -> 20102 if you specify an interval value that is too short (excluding the expected interval of the type keyword), MySQL assumes that you have saved the leftmost portion of the interval value. For example, if you specify a type of day_second, the value of expr is expected to be one day, hour, minute, and second. If you specify a value like "", MySQL assumes that the days and hours are lost and the value represents minutes and seconds. In other words, "" day_second is interpreted as it is equivalent to "" minute_second, this interpretation of MySQL's time value indicates that the elapsed time is not as a one-day time. If you use an incorrect date, the result is null. If you increase month, year_month, or year and the result date is greater than the maximum number of days of the new month, the day is adjusted by the maximum number of days of the new moon. Mysql> select date_add ('2014-01-30 ', interval 1 month);-> 1998-02-28 note that from the previous example, the interval and type keywords are not case sensitive. To_days (date) indicates a date, and returns a number of days (from 0 years ). Mysql> select to_days (950501);-> 728779 mysql> select to_days ('2017-10-07 ');-> 1997 to_days () is not intended to be used to use the glipro calendar (729669) the value before it appears. From_days (n) returns a day N and a date value. Mysql> select from_days (729669);-> '2017-10-07 'date_format (date, format) format the date value based on the format string. The following modifier can be used in the format string: % m month name (January ...... December) % W name of the Week (Sunday ...... Saturday) % d Date of the month with an English prefix (1st, 2nd, 3rd, and so on .) % Y year, number, 4 digits % Y year, number, 2 digits % A abbreviation of the week name (Sun ...... Sat) % d number of days in the month (00 ...... 31) % E number of days in the month (0 ...... 31) % m month, number (01 ...... 12) % C month, number (1 ...... 12) % B abbreviated month name (Jan ...... Dec) % J days in a year (001 ...... 366) % H hour (00 ...... 23) % K hour (0 ...... 23) % H hour (01 ...... 12) % I hour (01 ...... 12) % L hour (1 ...... 12) % I minute, number (00 ...... 59) % R time, 12 hours (HH: mm: ss [AP] m) % t time, 24 hours (HH: mm: SS) % s seconds (00 ...... 59) % s seconds (00 ...... 59) % P am or PM % W days in a week (0 = Sunday ...... 6 = Saturday) % u Week (0 ...... 52), here Sunday is the first day of the week % u Week (0 ...... 52), Monday is the first day of the week % A text "% ". All other characters are not interpreted and copied to the result. Mysql> select date_format ('2017-10-04 22:23:00 ',' % w % m % y '); -> 'Saturday October 100' mysql> select date_format ('2017-10-04 22:23:00 ',' % H: % I: % s');-> '22: 23: 00 'mysql> select date_format ('2017-10-04 22:23:00 ',' % d % Y % A % d % m % B % J '); -> '4th 97 sat 04 10 Oct 277 'mysql> select date_format ('2017-10-04 22:23:00 ', '% H % K % I % R % T % S % W');-> '22 22 10 10:23:00 PM 22:23:00 6' mysql3.23, in the format Modifier % Before the character. In earlier MySQL versions, % is optional. For example, select str_to_date (time field name, '% Y-% m-% D') from newstime_format (time, format), which is used like the preceding date_format () function, however, the format string can only contain format modifiers for processing hours, minutes, and seconds. Other modifiers generate a null value or 0. Curdate () current_date returns today's date value in 'yyyy-MM-DD 'or yyyymmdd format, depending on whether the function is used in a string or numeric context. Mysql> select curdate ();-> '2014-12-15 'mysql> select curdate () + 0;-> 1997 curtime () current_time to 'hh: mm: the current time value is returned in ss' or hhmmss format, depending on whether the function is used in a string or in the context of a number. Mysql> select curtime ();-> '23: 50: 26' mysql> select curtime () + 0;-> 235026 now () sysdate () current_timestamp returns the current date and time in 'yyyy-MM-DD hh: mm: ss' or yyyymmddhhmmss format, depending on whether the function is used in a string or in the context of a number. Mysql> select now ();-> '2014-12-15 23:50:26 'mysql> select now () + 0;-> 1997 unix_timestamp () unix_timestamp (date) if no parameter is called, a Unix timestamp (in seconds starting from '2017-01-01 00:00:00 'GMT) is returned ). If unix_timestamp () is called with a date parameter, it returns the second value starting from '2017-01-01 00:00:00 'GMT. Date can be a number of a date string, A datetime string, a timestamp, or a local time in yymmdd or yyyymmdd format. Mysql> select unix_timestamp ();-> 882226357 mysql> select unix_timestamp ('2017-10-04 22:23:00 ');-> 1997 when unix_timestamp is used for a timestamp column, the function will directly accept the value without the implicit "string-to-Unix-timestamp" transformation. From_unixtime (unix_timestamp) returns the value represented by the unix_timestamp parameter in 'yyyy-MM-DD hh: mm: ss' or yyyymmddhhmmss format, depending on whether the function is used in a string or numeric context. Mysql> select from_unixtime (875996580);-> '2017-10-04 22:23:00 'mysql> select from_unixtime (1997) + 0;-> 875996580 from_unixtime (unix_timestamp, Format) returns a string representing the Unix time mark, formatted according to the format string. Format can contain the same modifier as the entries listed by the date_format () function. Mysql> select from_unixtime (unix_timestamp (), '% Y % d % m % H: % I: % S % x '); -> '2014 23rd December 03:43:30 x' sec_to_time (seconds) returns the seconds parameter, which is converted to the hour, minute, and second. The value is formatted in 'hh: mm: ss' or hhmmss, it depends on whether the function is used in a string or a number context. Mysql> select sec_to_time (2378);-> '00: 39: 38' mysql> select sec_to_time (2378) + 0;-> 3938 time_to_sec (time) return the time parameter, convert to seconds. Mysql> select time_to_sec ('22: 23: 00');-> 80580 mysql> select time_to_sec ('00: 39: 38');-> 2378
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.