DAYOFWEEK(date)
returns the date date is the day of the week (1=Sunday, 2=Monday,...7=Saturday, ODBC standard)
mysql> select DAYOFWEEK('1998-02-03');
-> 3
WEEKDAY(date)
The return date date is the day of the week (0=Monday, 1=Tuesday, ... 6=Sunday).
mysql> select WEEKDAY('1997-10-04 22:23:00');
-> 5
mysql> select WEEKDAY('1997-11-05');
-> 2
DAYOFMONTH(date)
returns the date in January Day of the year (in the range of 1 to 31)
mysql> select DAYOFMONTH('1998-02-03');
-> 3
DAYOFYEAR(date)
returns the day of the year (in the range of 1 to 366)
mysql> select DAYOFYEAR('1998-02-03');
-> 34
MONTH(date)
returns the month value in date
mysql> select MONTH('
DAYNAME(date)
returns the date of the week (returned by English name)
mysql> select DAYNAME("1998-02-05");
-> 'Thursday'
MONTHNAME(date)
returns the date of the month (returned by English name)
mysql > select MONTHNAME("1998-02-05");
-> 'February'
QUARTER(date)
returns date is the quarter of the year
mysql> select QUARTER('98-04-01');
-> 2
WEEK (date, first)
returns the week of the year where date is (default value of first is 0, first value of 1 means that Monday is the start of the week, and 0 starts from Sunday)
mysql> select WEEK('1998-02-20') ;
-> 7
mysql> select WEEK('1998-02-20',0);
-> 7
mysql> select WEEK('1998-02-20',1);
-> 8
YEAR(date)
returns the year of date (range 1000 to 9999)
mysql> select YEAR('98-02-03');
-> 1998
HOUR(time)
returns the hours of time (range is 0 to 23)
mysql> select HOUR('10:05:03');
-> 10
MINUTE(time)
returns the minutes of time (range is 0 to 59)
mysql> select MINUTE('98-02-03 10:05:03');
-> 5
SECOND(time)
returns the seconds of time (range is 0 to 59)
mysql> select SECOND('10:05:03');
-> 3
PERIOD_ADD(P,N)
adds N months to period P and returns (P in the format YYMM or YYYYMM)
mysql> select PERIOD_ADD(9801 ,2);
-> 199803
PERIOD_DIFF(P1,P2)
returns the number of months between periods P1 and P2 (the format YYMM or YYYYMM for P1 and P2)
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)
Add and subtract date and time
(ADDDATE() and SUBDATE() are DATE_ADD () and DATE_SUB() are synonyms, and can also be used with the operator and - instead of the function
date is a DATETIME or DATE value, expr is an expression string type that adds and subtracts date to date specifying how the expression expr should be interpreted
[ type value meaning expected expr format]:
SECOND seconds SECONDS
MINUTE minutes MINUTES
HOUR time HOURS
DAY days DAYS
MONTH months MONTHS
YEAR years YEARS
MINUTE_SECOND minutes and seconds "MINUTES:SECONDS"
HOUR_MINUTE hours and minutes "HOURS:MINUTES"
DAY_HOUR days and hours" DAYS HOURS"
YEAR_MONTH year and month "YEARS-MONTHS"
HOUR_SECOND hours, minutes, "HOURS:MINUTES:SECONDS"
DAY_MINUTE days, hours, minutes "DAYS HOURS:MINUTES"
DAY_SECOND days, hours, minutes, seconds "DAYS HOURS:MINUTES:SECONDS"
Any punctuation is allowed in expr as a separator, if When all are DATE values, the result is a DATE value, otherwise the result is a DATETIME value)
If the type keyword is incomplete, MySQL takes the value from the right end, DAY_SECOND is equal to MINUTE_SECOND because of missing hours and minutes)
If MONTH, YEAR_MONTH or YEAR is added, the number of days is greater than The maximum number of days in the result month is the maximum number of days)
mysql> SELECT "1997-12-31 23:59:59" INTERVAL 1 SECOND;
-> 1998-01-01 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("1997-12-31 23:59:59",INTERVAL 1 SECOND);
-> 1998-01-01 00:00:00
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",INTERVAL 1 DAY);
-> 1998-01-01 23:59:59
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",INTERVAL "1:1" MINUTE_SECOND);
-> 1998-01-01 00:01:00
mysql> SELECT DATE_SUB("1998-01-01 00:00:00",INTERVAL "1 1:1:1" DAY_SECOND);
-> 1997-12-30 22:58:59
mysql> SELECT DATE_ADD("1998-01-01 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 "1999-07-02");
-> 1999
mysql>SELECT EXTRACT(YEAR_MONTH FROM "1999-07-02 01:02:03");
-> 199907
mysql> SELECT EXTRACT(DAY_MINUTE FROM "1999-07-02 01:02:03");
-> 20102
TO_DAYS(date)
returns how many days the date is from AD 0 to the present (not counting before 1582)
mysql> select TO_DAYS( 950501);
-> 728779
mysql> select TO_DAYS('1997-10-07');
-> 729669
FROM_DAYS(N)
gives the number of days from AD 0 to the present to return the DATE value (not counting before 1582)
mysql> select FROM_DAYS( 729669);
-> '1997-10-07'
DATE_FORMAT(date,format) format the
date value according to
the format string (the available identifiers in the format string:
%M month name (January...December)
%W week name (Sunday...Saturday)
%D The day of the month with the English prefix (1st, 2nd, 3rd, etc.)
%Y year, number, 4 digits
%y year, number, 2-digit
%a abbreviated week name (Sun...Sat)
%d day of month, number(00...31)
%e day of month, number(0...31)
%m month, number(01...12)
%c month, number(1...12)
%b Abbreviated month name (Jan…Dec)
%j Day of the 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 hour(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 of the week (0=Sunday...6=Saturday )
%U week (0...52), where Sunday is the first day of the week
%u week (0...52), where Monday is the first day of the week
%% character% )
mysql> select DATE_FORMAT('1997 -10-04 22:23:00','%W %M %Y');
-> 'Saturday October 1997'
mysql> select DATE_FORMAT('1997-10-04 22:23:00','%H:%i:%s');
-> '22:23:00'
mysql> select DATE_FORMAT ('1997-10-04 22:23:00','%D %y %a %d %m %b %j');
-> '4th 97 Sat 04 10 Oct 277'
mysql> select DATE_FORMAT('1997 -10-04 22:23:00','%H %k %I %r %T %S %w');
-> '22 22 10 10:23:00 PM 22:23:00 00 6'
TIME_FORMAT (time, format)
is similar to DATE_FORMAT(), but TIME_FORMAT only handles hours, minutes and seconds (the rest of the symbols produce a NULL value or 0)
CURDATE()
CURRENT_DATE()
returns the current date in 'YYYY-MM-DD' or YYYYMMDD format Value (string or number according to the context of the return value)
mysql> select CURDATE();
-> '1997-12-15'
mysql> select CURDATE() 0;
-> 19971215
CURTIME()
CURRENT_TIME()
returns the current time value in 'HH:MM:SS' or HHMMSS format (string or number according to the context of the returned value)
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 (string or number depending on the context of the returned value)
mysql> select NOW();
-> '1997-12-15 23:50:26'
mysql> select NOW() 0;
-> 19971215235026
UNIX_TIMESTAMP()
UNIX_TIMESTAMP(date)
returns a Unix timestamp (from '1970-01) -01 00:00:00'The number of seconds since GMT, the default value of date is the current time)
mysql> select UNIX_TIMESTAMP();
-> 882226357
mysql> select UNIX_TIMESTAMP('1997-10-04 22:23:00');
-> 875996580
FROM_UNIXTIME(unix_timestamp)
returns the value of the timestamp in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format (string or number depending on the context of the returned value)
mysql> select FROM_UNIXTIME(875996580);
- > '1997-10-04 22:23:00'
mysql> select FROM_UNIXTIME(875996580) 0;
-> 19971004222300
FROM_UNIXTIME(unix_timestamp,format)
returns the value of the timestamp in format string format
mysql> select FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y %D %M %h:%i:%s %x');
-> '1997 23rd December 03:43:30 x'
SEC_TO_TIME(seconds)
returns seconds in 'HH:MM:SS' or HHMMSS format The TIME value converted from the number (string or number according to the context of the return value)
mysql> select SEC_TO_TIME(2378);
-> '00:39:38'
mysql> select SEC_TO_TIME(2378) 0;
-> 3938
How many seconds does TIME_TO_SEC(time) return the time value
mysql> select TIME_TO_SEC('22:23:00');
-> 80580
mysql> select TIME_TO_SEC('00:39:38');
-> 2378
returns the date date is the day of the week (1=Sunday, 2=Monday,...7=Saturday, ODBC standard)
mysql> select DAYOFWEEK('1998-02-03');
-> 3
WEEKDAY(date)
The return date date is the day of the week (0=Monday, 1=Tuesday, ... 6=Sunday).
mysql> select WEEKDAY('1997-10-04 22:23:00');
-> 5
mysql> select WEEKDAY('1997-11-05');
-> 2
DAYOFMONTH(date)
returns the date in January Day of the year (in the range of 1 to 31)
mysql> select DAYOFMONTH('1998-02-03');
-> 3
DAYOFYEAR(date)
returns the day of the year (in the range of 1 to 366)
mysql> select DAYOFYEAR('1998-02-03');
-> 34
MONTH(date)
returns the month value in date
mysql> select MONTH('
DAYNAME(date)
returns the date of the week (returned by English name)
mysql> select DAYNAME("1998-02-05");
-> 'Thursday'
MONTHNAME(date)
returns the date of the month (returned by English name)
mysql > select MONTHNAME("1998-02-05");
-> 'February'
QUARTER(date)
returns date is the quarter of the year
mysql> select QUARTER('98-04-01');
-> 2
WEEK (date, first)
returns the week of the year where date is (default value of first is 0, first value of 1 means that Monday is the start of the week, and 0 starts from Sunday)
mysql> select WEEK('1998-02-20') ;
-> 7
mysql> select WEEK('1998-02-20',0);
-> 7
mysql> select WEEK('1998-02-20',1);
-> 8
YEAR(date)
returns the year of date (range 1000 to 9999)
mysql> select YEAR('98-02-03');
-> 1998
HOUR(time)
returns the hours of time (range is 0 to 23)
mysql> select HOUR('10:05:03');
-> 10
MINUTE(time)
returns the minutes of time (range is 0 to 59)
mysql> select MINUTE('98-02-03 10:05:03');
-> 5
SECOND(time)
returns the seconds of time (range is 0 to 59)
mysql> select SECOND('10:05:03');
-> 3
PERIOD_ADD(P,N)
adds N months to period P and returns (P in the format YYMM or YYYYMM)
mysql> select PERIOD_ADD(9801 ,2);
-> 199803
PERIOD_DIFF(P1,P2)
returns the number of months between periods P1 and P2 (the format YYMM or YYYYMM for P1 and P2)
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)
Add and subtract date and time
(ADDDATE() and SUBDATE() are DATE_ADD () and DATE_SUB() are synonyms, and can also be used with the operator and - instead of the function
date is a DATETIME or DATE value, expr is an expression string type that adds and subtracts date to date specifying how the expression expr should be interpreted
[ type value meaning expected expr format]:
SECOND seconds SECONDS
MINUTE minutes MINUTES
HOUR time HOURS
DAY days DAYS
MONTH months MONTHS
YEAR years YEARS
MINUTE_SECOND minutes and seconds "MINUTES:SECONDS"
HOUR_MINUTE hours and minutes "HOURS:MINUTES"
DAY_HOUR days and hours" DAYS HOURS"
YEAR_MONTH year and month "YEARS-MONTHS"
HOUR_SECOND hours, minutes, "HOURS:MINUTES:SECONDS"
DAY_MINUTE days, hours, minutes "DAYS HOURS:MINUTES"
DAY_SECOND days, hours, minutes, seconds "DAYS HOURS:MINUTES:SECONDS"
Any punctuation is allowed in expr as a separator, if When all are DATE values, the result is a DATE value, otherwise the result is a DATETIME value)
If the type keyword is incomplete, MySQL takes the value from the right end, DAY_SECOND is equal to MINUTE_SECOND because of missing hours and minutes)
If MONTH, YEAR_MONTH or YEAR is added, the number of days is greater than The maximum number of days in the result month is the maximum number of days)
mysql> SELECT "1997-12-31 23:59:59" INTERVAL 1 SECOND;
-> 1998-01-01 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("1997-12-31 23:59:59",INTERVAL 1 SECOND);
-> 1998-01-01 00:00:00
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",INTERVAL 1 DAY);
-> 1998-01-01 23:59:59
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",INTERVAL "1:1" MINUTE_SECOND);
-> 1998-01-01 00:01:00
mysql> SELECT DATE_SUB("1998-01-01 00:00:00",INTERVAL "1 1:1:1" DAY_SECOND);
-> 1997-12-30 22:58:59
mysql> SELECT DATE_ADD("1998-01-01 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 "1999-07-02");
-> 1999
mysql>SELECT EXTRACT(YEAR_MONTH FROM "1999-07-02 01:02:03");
-> 199907
mysql> SELECT EXTRACT(DAY_MINUTE FROM "1999-07-02 01:02:03");
-> 20102
TO_DAYS(date)
returns how many days the date is from AD 0 to the present (not counting before 1582)
mysql> select TO_DAYS( 950501);
-> 728779
mysql> select TO_DAYS('1997-10-07');
-> 729669
FROM_DAYS(N)
gives the number of days from AD 0 to the present to return the DATE value (not counting before 1582)
mysql> select FROM_DAYS( 729669);
-> '1997-10-07'
DATE_FORMAT(date,format) format the
date value according to
the format string (the available identifiers in the format string:
%M month name (January...December)
%W week name (Sunday...Saturday)
%D The day of the month with the English prefix (1st, 2nd, 3rd, etc.)
%Y year, number, 4 digits
%y year, number, 2-digit
%a abbreviated week name (Sun...Sat)
%d day of month, number(00...31)
%e day of month, number(0...31)
%m month, number(01...12)
%c month, number(1...12)
%b Abbreviated month name (Jan…Dec)
%j Day of the 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 hour(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 of the week (0=Sunday...6=Saturday )
%U week (0...52), where Sunday is the first day of the week
%u week (0...52), where Monday is the first day of the week
%% character% )
mysql> select DATE_FORMAT('1997 -10-04 22:23:00','%W %M %Y');
-> 'Saturday October 1997'
mysql> select DATE_FORMAT('1997-10-04 22:23:00','%H:%i:%s');
-> '22:23:00'
mysql> select DATE_FORMAT ('1997-10-04 22:23:00','%D %y %a %d %m %b %j');
-> '4th 97 Sat 04 10 Oct 277'
mysql> select DATE_FORMAT('1997 -10-04 22:23:00','%H %k %I %r %T %S %w');
-> '22 22 10 10:23:00 PM 22:23:00 00 6'
TIME_FORMAT (time, format)
is similar to DATE_FORMAT(), but TIME_FORMAT only handles hours, minutes and seconds (the rest of the symbols produce a NULL value or 0)
CURDATE()
CURRENT_DATE()
returns the current date in 'YYYY-MM-DD' or YYYYMMDD format Value (string or number according to the context of the return value)
mysql> select CURDATE();
-> '1997-12-15'
mysql> select CURDATE() 0;
-> 19971215
CURTIME()
CURRENT_TIME()
returns the current time value in 'HH:MM:SS' or HHMMSS format (string or number according to the context of the returned value)
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 (string or number depending on the context of the returned value)
mysql> select NOW();
-> '1997-12-15 23:50:26'
mysql> select NOW() 0;
-> 19971215235026
UNIX_TIMESTAMP()
UNIX_TIMESTAMP(date)
returns a Unix timestamp (from '1970-01) -01 00:00:00'The number of seconds since GMT, the default value of date is the current time)
mysql> select UNIX_TIMESTAMP();
-> 882226357
mysql> select UNIX_TIMESTAMP('1997-10-04 22:23:00');
-> 875996580
FROM_UNIXTIME(unix_timestamp)
returns the value of the timestamp in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format (string or number depending on the context of the returned value)
mysql> select FROM_UNIXTIME(875996580);
- > '1997-10-04 22:23:00'
mysql> select FROM_UNIXTIME(875996580) 0;
-> 19971004222300
FROM_UNIXTIME(unix_timestamp,format)
returns the value of the timestamp in format string format
mysql> select FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y %D %M %h:%i:%s %x');
-> '1997 23rd December 03:43:30 x'
SEC_TO_TIME(seconds)
returns seconds in 'HH:MM:SS' or HHMMSS format The TIME value converted from the number (string or number according to the context of the return value)
mysql> select SEC_TO_TIME(2378);
-> '00:39:38'
mysql> select SEC_TO_TIME(2378) 0;
-> 3938
How many seconds does TIME_TO_SEC(time) return the time value
mysql> select TIME_TO_SEC('22:23:00');
-> 80580
mysql> select TIME_TO_SEC('00:39:38');
-> 2378