The DATE and TIME values are represented using DATE, TIME, DATETIME, TIMESTAMP and YEAR data type. Each type has a range of valid values including the ZERO value.The TIMESTAMP has special auto update behavior.
The following table shows the format of the zero value for each type.
Data Type | “Zero” Value |
---|---|
'0000-00-00' | |
'00:00:00' | |
'0000-00-00 00:00:00' | |
'0000-00-00 00:00:00' | |
0000 |
There are different DATE and TIME functions that can be used to manipulate temporal values.
Name | Description |
---|---|
Add time values (intervals) to a date value | |
Add time | |
Convert from one timezone to another | |
Return the current date | |
Synonyms for CURDATE() | |
Synonyms for CURTIME() | |
Synonyms for NOW() | |
Return the current time | |
Add time values (intervals) to a date value | |
Format date as specified | |
Subtract a time value (interval) from a date | |
Extract the date part of a date or datetime expression | |
Subtract two dates | |
Synonym for DAYOFMONTH() | |
Return the name of the weekday | |
Return the day of the month (0-31) | |
Return the weekday index of the argument | |
Return the day of the year (1-366) | |
Extract part of a date | |
Convert a day number to a date | |
Format UNIX timestamp as a date | |
Return a date format string | |
Extract the hour | |
Return the last day of the month for the argument | |
Synonym for NOW() | |
Synonym for NOW() | |
Create a date from the year and day of year | |
Create time from hour, minute, second | |
Return the microseconds from argument | |
Return the minute from the argument | |
Return the month from the date passed | |
Return the name of the month | |
Return the current date and time | |
Add a period to a year-month | |
Return the number of months between periods | |
Return the quarter from a date argument | |
Converts seconds to 'HH:MM:SS' format | |
Return the second (0-59) | |
Convert a string to a date | |
Synonym for DATE_SUB() when invoked with three arguments | |
Subtract times | |
Return the time at which the function executes | |
Format as time | |
Return the argument converted to seconds | |
Extract the time portion of the expression passed | |
Subtract time | |
With a single argument, this function returns the date or datetime expression; with two arguments, the sum of the arguments | |
Add an interval to a datetime expression | |
Subtract an interval from a datetime expression | |
Return the date argument converted to days | |
Return the date or datetime argument converted to seconds since Year 0 | |
Return a UNIX timestamp | |
Return the current UTC date | |
Return the current UTC time | |
Return the current UTC date and time | |
Return the week number | |
Return the weekday index | |
Return the calendar week of the date (0-53) | |
Return the year | |
Return the year and week |
We will look at some of the functions which have been mentioned in above table:
1. DATE(expr):
Example:
mysql> SELECT DATE('2014-02-10'); +---------------------------+ | DATE('2014-02-10') | +---------------------------+ | 2014-02-10 | +---------------------------+ 1 row in set (0.00 sec) mysql> SELECT DATE('2014-02-10 01:20-02'); +-----------------------------------------------+ | DATE('2014-02-10 01:20-02') | +-----------------------------------------------+ | 2014-02-10 | +-----------------------------------------------+ 1 row in set (0.00 sec)
2. DATEDIFF(Expr1, expr2):
Gives the difference between the date parts in days.
mysql> SELECT DATEDIFF('2000-10-02','2000-09-01'); +--------------------------------------------------------+ | DATEDIFF('2000-10-02','2000-09-01') | +--------------------------------------------------------+ | 31 | +--------------------------------------------------------+ 1 row in set (0.03 sec) mysql> SELECT DATEDIFF('2000-10-02','2000-11-01'); +--------------------------------------------------------+ | DATEDIFF('2000-10-02','2000-11-01') | +--------------------------------------------------------+ | -30 | +--------------------------------------------------------+ 1 row in set (0.00 sec)
3. DAYNAME(Date)
This function returns the day of the month for the date. And will return number between 1 and 31.
mysql> SELECT DAYOFMONTH('2002-02-29'); +----------------------------------------------+ | DAYOFMONTH('2002-02-29') | +----------------------------------------------+ | NULL | +----------------------------------------------+ 1 row in set, 1 warning (0.06 sec) mysql> SELECT DAYOFMONTH('2002-02-28'); +----------------------------------------------+ | DAYOFMONTH('2002-02-28') | +-----------------------------------------------+ | 28 | +-----------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT DAYOFMONTH('2004-02-29'); +----------------------------------------------+ | DAYOFMONTH('2004-02-29') | +----------------------------------------------+ | 29 | +----------------------------------------------+ 1 row in set (0.00 sec)
In the first example, as 2002-02-29 was not a leap year, the output was given as NULL.
In the third example, 2004-02-29 being a leap year, 29 day was displayed.
Similar to DAYOFMONTH(date), there is DAYOFWEEK(date) which gives output in range of 1 to 7 and DAYSOFYEAR(date) which gives output in range of 1 to 366.
4. DATE_FORMAT(date,format)
This function will format the date value according to the format specified. The formats can be specified as below. % should be preceeding the character.
Specifier | Description |
%a | Abbreviated weekday name (Sun..Sat) |
%b | Abbreviated month name (Jan..Dec) |
%c | Month, numeric (0..12) |
%D | Day of the month with English suffix (0th, 1st, 2nd, 3rd, .) |
%d | Day of the month, numeric (00..31) |
%e | Day of the month, numeric (0..31) |
%f | Microseconds (000000..999999) |
%H | Hour (00..23) |
%h | Hour (01..12) |
%I | Hour (01..12) |
%i | Minutes, numeric (00..59) |
%j | Day of year (001..366) |
%k | Hour (0..23) |
%l | Hour (1..12) |
%M | Month name (January..December) |
%m | Month, numeric (00..12) |
%p | AM or PM |
%r | Time, 12-hour (hh:mm:ss followed by AM or PM) |
%S | Seconds (00..59) |
%s | Seconds (00..59) |
%T | Time, 24-hour (hh:mm:ss) |
%U | Week (00..53), where Sunday is the first day of the week |
%u | Week (00..53), where Monday is the first day of the week |
%V | Week (01..53), where Sunday is the first day of the week; used with %X |
%v | Week (01..53), where Monday is the first day of the week; used with %x |
%W | Weekday name (Sunday..Saturday) |
%w | Day of the week (0=Sunday..6=Saturday) |
%X | Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V |
%x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v |
%Y | Year, numeric, four digits |
%y | Year, numeric (two digits) |
%% | A literal .%. character |
%x | x, for any.x. not listed above |
mysql> SELECT DATE_FORMAT('2010-07-23','%Y %b %d'); +------------------------------------------------------------------+ | DATE_FORMAT('2010-07-23','%Y %b %d') | +------------------------------------------------------------------+ | 2010 Jul 23 | +------------------------------------------------------------------+ 1 row in set (0.05 sec)
5. DATE_ADD(date, INTERVAL units) and DATE_SUB(date, INTERVAL units)
These functions perform the date arithmetic that is addition and subtraction of dates by the interval mentioned. The interval can be of days, months, years, seconds etc. The intervals that can be given are as below:
unit Value | ExpectedexprFormat |
MICROSECOND | MICROSECONDS |
SECOND | SECONDS |
MINUTE | MINUTES |
HOUR | HOURS |
DAY | DAYS |
WEEK | WEEKS |
MONTH | MONTHS |
QUARTER | QUARTERS |
YEAR | YEARS |
SECOND_MICROSECOND | 'SECONDS.MICROSECONDS' |
MINUTE_MICROSECOND | 'MINUTES.MICROSECONDS' |
MINUTE_SECOND | 'MINUTES:SECONDS' |
HOUR_MICROSECOND | 'HOURS.MICROSECONDS' |
HOUR_SECOND | 'HOURS:MINUTES:SECONDS' |
HOUR_MINUTE | 'HOURS:MINUTES' |
DAY_MICROSECOND | 'DAYS.MICROSECONDS' |
DAY_SECOND | 'DAYS HOURS:MINUTES:SECONDS' |
DAY_MINUTE | 'DAYS HOURS:MINUTES' |
DAY_HOUR | 'DAYS HOURS' |
YEAR_MONTH | 'YEARS-MONTHS' |
mysql> SELECT DATE_ADD('2001-01-01',INTERVAL 1 DAY); +------------------------------------------------------------------+ | DATE_ADD('2001-01-01',INTERVAL 1 DAY) | +------------------------------------------------------------------+ | 2001-01-02 | +------------------------------------------------------------------+ 1 row in set (0.02 sec) Here the date was advanced by 1 day. mysql> SELECT DATE_SUB('2001-01-01',INTERVAL 1 MONTH); +------------------------------------------------------------------+ | DATE_SUB('2001-01-01',INTERVAL 1 MONTH) | +------------------------------------------------------------------+ | 2000-12-01 | +------------------------------------------------------------------+ 1 row in set (0.00 sec) Here the month was subtracted by 1. mysql> SELECT DATE_ADD('2001-03-03 21:23:01', INTERVAL '5:4' MINUTE_SECOND); +---------------------------------------------------------------------------------------------------------+ | DATE_ADD('2001-03-03 21:23:01', INTERVAL '5:4' MINUTE_SECOND) | +---------------------------------------------------------------------------------------------------------+ | 2001-03-03 21:28:05 | +---------------------------------------------------------------------------------------------------------+ 1 row in set (0.03 sec)