MySQL
For the value range of each type and the valid format of the specified date value, see 7.3.6 Date and Time type.
Here is an example of using the date function. The following query Selects all records. The value of date_col is within the last 30 days:
Mysql> select something from table
Where to_days (now ()-to_days (date_col) <= 30;
Dayofweek (date)
Returns the index of the week of the date (1 = Sunday, 2 = Monday ,...... 7 = Saturday ). These index values correspond to the ODBC standard.
Mysql> select dayofweek ('2017-02-03 ');
-> 3
Weekday (date)
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 ('2017-02-03 ');
-> 3
Dayofyear (date)
Returns the number of days in a year from 1 to 366.
Mysql> select dayofyear ('2017-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 week name of date.
Mysql> select dayname ("1998-02-05 ");
-> 'Thursday'
Monthname (date)
Returns the month name of date.
Mysql> select monthname ("1998-02-05 ");
-> 'February'
Quarter (date)
Returns the quarter of a year from date, ranging from 1 to 4.
Mysql> select quarter ('98-04-01 ');
-> 2
Week (date)
Week (date, first)
If Sunday is the first day of a week, there is a single parameter that returns the number of weeks of the date, ranging from 0 to 52. Two Parameter formats: Week ()
Specify whether the week starts on Sunday or Monday. If the second parameter is 0 and the week starts from Sunday, if the second parameter is 1,
Starting from Monday.
Mysql> select Week ('2017-02-20 ');
-> 7
Mysql> select Week ('2017-02-20 ', 0 );
-> 7
Mysql> select Week ('2017-02-20 ', 1 );
-> 8
Year (date)
Returns the year of date, ranging from 1000 to 9999.
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)
The number of seconds for the return time, 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 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 ("23:59:59 ",
Interval 1 second );
-> 00:00:00
Mysql> select date_add ("23:59:59 ",
Interval 1 day );
-> 23:59:59
Mysql> select date_add ("23:59:59 ",
Interval "1:1" minute_second );
-> 00:01:00
Mysql> select date_sub ("00:00:00 ",
Interval "1" day_second );
-> 1997-12-30 22:58:59
Mysql> select date_add ("00:00:00 ",
Interval "-1 10" day_hour );
-> 1997-12-30 14:00:00
Mysql> select date_sub ("1998-01-02", interval 31 day );
-> 1997-12-02
Mysql> select extract (year from "maid ");
-> 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 values represent minutes and seconds. In other words, "" day_second is equivalent to "" minute_second
For MySQL, the time value represents the elapsed time, rather than the time of the day. 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 ('2017-01-30 ', interval 1 month );
-> 1998-02-28
Note: In the previous example, the interval and type keywords are not case sensitive.
To_days (date)
Returns the number of days (from 0 years ).
Mysql> select to_days (950501 );
-> 728779
Mysql> select to_days ('2017-10-07 ');
-> 729669
To_days () is not intended to be used to use the value before the occurrence of the glipro calendars (1582.
From_days (N)
Returns a date value for the number of days n.
Mysql> select from_days (729669 );
-> '2017-10-07'
To_days () is not intended to be used to use the value before the occurrence of the glipro calendars (1582.
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 indicates the 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 abbreviated name of the Week (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). 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 1997'
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 123'
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'
In mysql3.23, % is required before the format modifier. In earlier MySQL versions, % is optional.
Time_format (time, Format)
This is used like the preceding date_format () function, but 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 ();
-> '2017-12-15'
Mysql> select curdate () + 0;
-> 19971215
Curtime ()
Current_time
Returns the current time value in 'hh: mm: 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 in a string or in a number
The context is used.
Mysql> select now ();
-> '2017-12-15 23:50:26'
Mysql> select now () + 0;
-> 19971215235026
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 () Uses
The date parameter is called. It returns the second value starting from '2017-01-01 00:00:00 'GMT. Date can be a date string, A datetime
String, a timestamp, or a number in the local time format of yymmdd or yyyymmdd.
Mysql> select unix_timestamp ();
-> 882226357
Mysql> select unix_timestamp ('2017-10-04 22:23:00 ');
-> 875996580
When unix_timestamp is used in a timestamp column, the function accepts the value directly 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 the function being in a string
Or a digital context.
Mysql> select from_unixtime (875996580 );
-> '2017-10-04 22:23:00'
Mysql> select from_unixtime (875996580) + 0;
-> 19971004222300
From_unixtime (unix_timestamp, Format)
Returns a string representing the Unix time mark, formatted according to the format string. Format can contain entries listed with the date_format () function
The same modifier.
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 hour, minute, and second. The value is formatted in 'hh: mm: ss' or hhmmss, depending on whether the function is in a string or number.
Is Used in the context.
Mysql> select sec_to_time (2378 );
-> '00: 39: 38'
Mysql> select sec_to_time (2378) + 0;
-> 3938
Time_to_sec (time)
Returns the time parameter, which is converted to seconds.
Mysql> select time_to_sec ('22: 23: 00 ');
-> 80580
Mysql> select time_to_sec ('00: 39: 38 ');
-> 2378 MySQL
For the value range of each type and the valid format of the specified date value, see 7.3.6 Date and Time type.
Here is an example of using the date function. The following query Selects all records. The value of date_col is within the last 30 days:
Mysql> select something from table
Where to_days (now ()-to_days (date_col) <= 30;
Dayofweek (date)
Returns the index of the week of the date (1 = Sunday, 2 = Monday ,...... 7 = Saturday ). These index values correspond to the ODBC standard.
Mysql> select dayofweek ('2017-02-03 ');
-> 3
Weekday (date)
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 ('2017-02-03 ');
-> 3
Dayofyear (date)
Returns the number of days in a year from 1 to 366.
Mysql> select dayofyear ('2017-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 week name of date.
Mysql> select dayname ("1998-02-05 ");
-> 'Thursday'
Monthname (date)
Returns the month name of date.
Mysql> select monthname ("1998-02-05 ");
-> 'February'
Quarter (date)
Returns the quarter of a year from date, ranging from 1 to 4.
Mysql> select quarter ('98-04-01 ');
-> 2
Week (date)
Week (date, first)
If Sunday is the first day of a week, there is a single parameter that returns the number of weeks of the date, ranging from 0 to 52. Two Parameter formats: Week ()
Specify whether the week starts on Sunday or Monday. If the second parameter is 0 and the week starts from Sunday, if the second parameter is 1,
Starting from Monday.
Mysql> select Week ('2017-02-20 ');
-> 7
Mysql> select Week ('2017-02-20 ', 0 );
-> 7
Mysql> select Week ('2017-02-20 ', 1 );
-> 8
Year (date)
Returns the year of date, ranging from 1000 to 9999.
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)
The number of seconds for the return time, 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 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 ("23:59:59 ",
Interval 1 second );
-> 00:00:00
Mysql> select date_add ("23:59:59 ",
Interval 1 day );
-> 23:59:59
Mysql> select date_add ("23:59:59 ",
Interval "1:1" minute_second );
-> 00:01:00
Mysql> select date_sub ("00:00:00 ",
Interval "1" day_second );
-> 1997-12-30 22:58:59
Mysql> select date_add ("00:00:00 ",
Interval "-1 10" day_hour );
-> 1997-12-30 14:00:00
Mysql> select date_sub ("1998-01-02", interval 31 day );
-> 1997-12-02
Mysql> select extract (year from "maid ");
-> 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 values represent minutes and seconds. In other words, "" day_second is equivalent to "" minute_second
For MySQL, the time value represents the elapsed time, rather than the time of the day. 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 ('2017-01-30 ', interval 1 month );
-> 1998-02-28
Note: In the previous example, the interval and type keywords are not case sensitive.
To_days (date)
Returns the number of days (from 0 years ).
Mysql> select to_days (950501 );
-> 728779
Mysql> select to_days ('2017-10-07 ');
-> 729669
To_days () is not intended to be used to use the value before the occurrence of the glipro calendars (1582.
From_days (N)
Returns a date value for the number of days n.
Mysql> select from_days (729669 );
-> '2017-10-07'
To_days () is not intended to be used to use the value before the occurrence of the glipro calendars (1582.
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 indicates the 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 abbreviated name of the Week (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). 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 1997'
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 123'
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'
In mysql3.23, % is required before the format modifier. In earlier MySQL versions, % is optional.
Time_format (time, Format)
This is used like the preceding date_format () function, but 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 ();
-> '2017-12-15'
Mysql> select curdate () + 0;
-> 19971215
Curtime ()
Current_time
Returns the current time value in 'hh: mm: 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 in a string or in a number
The context is used.
Mysql> select now ();
-> '2017-12-15 23:50:26'
Mysql> select now () + 0;
-> 19971215235026
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 () Uses
The date parameter is called. It returns the second value starting from '2017-01-01 00:00:00 'GMT. Date can be a date string, A datetime
String, a timestamp, or a number in the local time format of yymmdd or yyyymmdd.
Mysql> select unix_timestamp ();
-> 882226357
Mysql> select unix_timestamp ('2017-10-04 22:23:00 ');
-> 875996580
When unix_timestamp is used in a timestamp column, the function accepts the value directly 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 the function being in a string
Or a digital context.
Mysql> select from_unixtime (875996580 );
-> '2017-10-04 22:23:00'
Mysql> select from_unixtime (875996580) + 0;
-> 19971004222300
From_unixtime (unix_timestamp, Format)
Returns a string representing the Unix time mark, formatted according to the format string. Format can contain entries listed with the date_format () function
The same modifier.
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 hour, minute, and second. The value is formatted in 'hh: mm: ss' or hhmmss, depending on whether the function is in a string or number.
Is Used in the context.
Mysql> select sec_to_time (2378 );
-> '00: 39: 38'
Mysql> select sec_to_time (2378) + 0;
-> 3938
Time_to_sec (time)
Returns the time parameter, which is converted to seconds.
Mysql> select time_to_sec ('22: 23: 00 ');
-> 80580
Mysql> select time_to_sec ('00: 39: 38 ');
-> 2378