Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
58 views

SQL - Date Functions: Name Desc Ription

The document provides a detailed list and description of SQL date and time functions available in MySQL. It lists over 50 functions including ADDDATE(), CURDATE(), DATE_FORMAT(), NOW(), and TIMESTAMP() among others and provides examples of how each function can be used.

Uploaded by

Assem Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

SQL - Date Functions: Name Desc Ription

The document provides a detailed list and description of SQL date and time functions available in MySQL. It lists over 50 functions including ADDDATE(), CURDATE(), DATE_FORMAT(), NOW(), and TIMESTAMP() among others and provides examples of how each function can be used.

Uploaded by

Assem Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

SQL - DATE FUNCTIONS

http://www.tuto rialspo int.co m/sql/sql-date -functio ns.htm Co pyrig ht © tuto rials po int.co m

Following is a list of all important Date and T ime related functions available throug h SQL. T here are various other
functions supported by your RDBMS. Given list is based on MySQL RDBMS.

Name Desc ription

ADDDAT E() Adds dates

ADDT IME() Adds time

CONVERT _T Z () Converts from one timezone to another

CURDAT E() Returns the current date

CURRENT _DAT E(), Synonyms for CURDAT E()


CURRENT _DAT E

CURRENT _T IME(), Synonyms for CURT IME()


CURRENT _T IME

CURRENT _T IMEST AMP(), Synonyms for NOW()


CURRENT _T IMEST AMP

CURT IME() Returns the current time

DAT E_ADD() Adds two dates

DAT E_FORMAT () Formats date as specified

DAT E_SUB() Subtracts two dates

DAT E() Extracts the date part of a date or datetime expression

DAT EDIFF() Subtracts two dates

DAY() Synonym for DAYOFMONT H()

DAYNAME() Returns the name of the weekday

DAYOFMONT H() Returns the day of the month (1-31)

DAYOFWEEK() Returns the weekday index of the arg ument

DAYOFYEAR() Returns the day of the year (1-366)

EXT RACT Extracts part of a date

FROM_DAYS() Converts a day number to a date

FROM_UNIXT IME() Formats date as a UNIX timestamp

HOUR() Extracts the hour

LAST _DAY Returns the last day of the month for the arg ument

LOCALT IME(), LOCALT IME Synonym for NOW()

LOCALT IMEST AMP, Synonym for NOW()


LOCALT IMEST AMP()
MAKEDAT E() Creates a date from the year and day of year

MAKET IME MAKET IME()

MICROSECOND() Returns the microseconds from arg ument

MINUT E() Returns the minute from the arg ument

MONT H() Return the month from the date passed

MONT HNAME() Returns the name of the month

NOW() Returns the current date and time

PERIOD_ADD() Adds a period to a year-month

PERIOD_DIFF() Returns the number of months between periods

QUART ER() Returns the quarter from a date arg ument

SEC_T O_T IME() Converts seconds to 'HH:MM:SS' format

SECOND() Returns the second (0-59)

ST R_T O_DAT E() Converts a string to a date

SUBDAT E() When invoked with three arg uments a synonym for DAT E_SUB()

SUBT IME() Subtracts times

SYSDAT E() Returns the time at which the function executes

T IME_FORMAT () Formats as time

T IME_T O_SEC() Returns the arg ument converted to seconds

T IME() Extracts the time portion of the expression passed

T IMEDIFF() Subtracts time

T IMEST AMP() With a sing le arg ument, this function returns the date or datetime
expression. With two arg uments, the sum of the arg uments

T IMEST AMPADD() Adds an interval to a datetime expression

T IMEST AMPDIFF() Subtracts an interval from a datetime expression

T O_DAYS() Returns the date arg ument converted to days

UNIX_T IMEST AMP() Returns a UNIX timestamp

UT C_DAT E() Returns the current UT C date

UT C_T IME() Returns the current UT C time

UT C_T IMEST AMP() Returns the current UT C date and time

WEEK() Returns the week number

WEEKDAY() Returns the weekday index

WEEKOFYEAR() Returns the calendar week of the date (1-53)

YEAR() Returns the year


YEARWEEK() Returns the year and week

ADDDATE(date,INTERVAL expr unit), ADDDATE(expr,days)


When invoked with the INT ERVAL form of the second arg ument, ADDDAT E() is a synonym for DAT E_ADD().
T he related function SUBDAT E() is a synonym for DAT E_SUB(). For information on the INT ERVAL unit
arg ument, see the discussion for DAT E_ADD().

mysql> SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);


+---------------------------------------------------------+
| DATE_ADD('1998-01-02', INTERVAL 31 DAY) |
+---------------------------------------------------------+
| 1998-02-02 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT ADDDATE('1998-01-02', INTERVAL 31 DAY);


+---------------------------------------------------------+
| ADDDATE('1998-01-02', INTERVAL 31 DAY) |
+---------------------------------------------------------+
| 1998-02-02 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

When invoked with the days form of the second arg ument, MySQL treats it as an integ er number of days to be
added to expr.

mysql> SELECT ADDDATE('1998-01-02', 31);


+---------------------------------------------------------+
| DATE_ADD('1998-01-02', INTERVAL 31 DAY) |
+---------------------------------------------------------+
| 1998-02-02 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

ADDTIME(expr1,expr2)
ADDT IME() adds expr2 to expr1 and returns the result. expr1 is a time or datetime expression, and expr2 is a
time expression.

mysql> SELECT ADDTIME('1997-12-31 23:59:59.999999','1 1:1:1.000002');


+---------------------------------------------------------+
| DATE_ADD('1997-12-31 23:59:59.999999','1 1:1:1.000002') |
+---------------------------------------------------------+
| 1998-01-02 01:01:01.000001 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CONVERT_TZ(dt,from_tz,to_tz)
T his converts a datetime value dt from the time zone g iven by from_tz to the time zone g iven by to_tz and
returns the resulting value. T his function returns NULL if the arg uments are invalid.

mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');


+---------------------------------------------------------+
| CONVERT_TZ('2004-01-01 12:00:00','GMT','MET') |
+---------------------------------------------------------+
| 2004-01-01 13:00:00 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');


+---------------------------------------------------------+
| CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00') |
+---------------------------------------------------------+
| 2004-01-01 22:00:00 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the
function is used in a string or numeric context.

mysql> SELECT CURDATE();


+---------------------------------------------------------+
| CURDATE() |
+---------------------------------------------------------+
| 1997-12-15 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT CURDATE() + 0;


+---------------------------------------------------------+
| CURDATE() + 0 |
+---------------------------------------------------------+
| 19971215 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CURRENT_DATE and CURRENT_DATE()


CURRENT _DAT E and CURRENT _DAT E() are synonyms for CURDAT E()

CURTIME()
Returns the current time as a value in 'HH:MM:SS' or HHMMSS format, depending on whether the function is
used in a string or numeric context. T he value is expressed in the current time zone.

mysql> SELECT CURTIME();


+---------------------------------------------------------+
| CURTIME() |
+---------------------------------------------------------+
| 23:50:26 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT CURTIME() + 0;


+---------------------------------------------------------+
| CURTIME() + 0 |
+---------------------------------------------------------+
| 235026 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

CURRENT_TIME and CURRENT_TIME()


CURRENT _T IME and CURRENT _T IME() are synonyms for CURT IME().

CURRENT_TIMESTAMP and CURRENT_TIMESTAMP()


CURRENT _T IMEST AMP and CURRENT _T IMEST AMP() are synonyms for NOW().

DATE(expr)
Extracts the date part of the date or datetime expression expr.

mysql> SELECT DATE('2003-12-31 01:02:03');


+---------------------------------------------------------+
| DATE('2003-12-31 01:02:03') |
+---------------------------------------------------------+
| 2003-12-31 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

DATEDIFF(expr1,expr2)
DAT EDIFF() returns expr1 . expr2 expressed as a value in days from one date to the other. expr1 and expr2
are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

mysql> SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');


+---------------------------------------------------------+
| DATEDIFF('1997-12-31 23:59:59','1997-12-30') |
+---------------------------------------------------------+
| 1 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr


unit)
T hese functions perform date arithmetic. date is a DAT ET IME or DAT E value specifying the starting date. expr
is an expression specifying the interval value to be added or subtracted from the starting date. expr is a string ; it
may start with a '-' for neg ative intervals. unit is a keyword indicating the units in which the expression should be
interpreted.

T he INT ERVAL keyword and the unit specifier are not case sensitive.

T he following table shows the expected form of the expr arg ument for each unit value;

unit Value Expec tedexprFormat

MICROSECOND MICROSECONDS

SECOND SECONDS

MINUT E MINUT ES

HOUR HOURS

DAY DAYS

WEEK WEEKS

MONT H MONT HS

QUART ER QUART ERS

YEAR YEARS

SECOND_MICROSECOND 'SECONDS.MICROSECONDS'

MINUT E_MICROSECOND 'MINUT ES.MICROSECONDS'

MINUT E_SECOND 'MINUT ES:SECONDS'

HOUR_MICROSECOND 'HOURS.MICROSECONDS'

HOUR_SECOND 'HOURS:MINUT ES:SECONDS'

HOUR_MINUT E 'HOURS:MINUT ES'

DAY_MICROSECOND 'DAYS.MICROSECONDS'

DAY_SECOND 'DAYS HOURS:MINUT ES:SECONDS'

DAY_MINUT E 'DAYS HOURS:MINUT ES'


DAY_HOUR 'DAYS HOURS'

YEAR_MONT H 'YEARS-MONT HS'

T he values QUART ER and WEEK are available beg inning with MySQL 5.0.0.

mysql> SELECT DATE_ADD('1997-12-31 23:59:59',


-> INTERVAL '1:1' MINUTE_SECOND);
+---------------------------------------------------------+
| DATE_ADD('1997-12-31 23:59:59', INTERVAL... |
+---------------------------------------------------------+
| 1998-01-01 00:01:00 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATE_ADD('1999-01-01', INTERVAL 1 HOUR);


+---------------------------------------------------------+
| DATE_ADD('1999-01-01', INTERVAL 1 HOUR) |
+---------------------------------------------------------+
| 1999-01-01 01:00:00 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

DATE_FORMAT(date,format)
Formats the date value according to the format string .

T he following specifiers may be used in the format string . T he '%' character is required before format specifier
characters.

Spec ifier Desc ription

%a Abbreviated weekday name (Sun..Sat)

%b Abbreviated month name (Jan..Dec)

%c Month, numeric (0..12)

%D Day of the month with Eng lish 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 T ime, 12-hour (hh:mm:ss followed by AM or PM)

%S Seconds (00..59)

%s Seconds (00..59)

%T T ime, 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 dig its; used with %V

%x Year for the week, where Monday is the first day of the week, numeric, four dig its; used with
%v

%Y Year, numeric, four dig its

%y Year, numeric (two dig its)

%% A literal .%. character

%x x, for any.x. not listed above

mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y');


+---------------------------------------------------------+
| DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y') |
+---------------------------------------------------------+
| Saturday October 1997 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00'


-> '%H %k %I %r %T %S %w');
+---------------------------------------------------------+
| DATE_FORMAT('1997-10-04 22:23:00....... |
+---------------------------------------------------------+
| 22 22 10 10:23:00 PM 22:23:00 00 6 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

DATE_SUB(date,INTERVAL expr unit)


T his is similar to DAT E_ADD() function.

DAY(date)
DAY() is a synonym for DAYOFMONT H().

DAYNAME(date)
Returns the name of the weekday for date.
mysql> SELECT DAYNAME('1998-02-05');
+---------------------------------------------------------+
| DAYNAME('1998-02-05') |
+---------------------------------------------------------+
| Thursday |
+---------------------------------------------------------+
1 row in set (0.00 sec)

DAYOFMONTH(date)
Returns the day of the month for date, in the rang e 0 to 31.

mysql> SELECT DAYOFMONTH('1998-02-03');


+---------------------------------------------------------+
| DAYOFMONTH('1998-02-03') |
+---------------------------------------------------------+
| 3 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

DAYOFWEEK(date)
Returns the weekday index for date (1 = Sunday, 2 = Monday, ., 7 = Saturday). T hese index values correspond to
the ODBC standard.

mysql> SELECT DAYOFWEEK('1998-02-03');


+---------------------------------------------------------+
|DAYOFWEEK('1998-02-03') |
+---------------------------------------------------------+
| 3 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

DAYOFYEAR(date)
Returns the day of the year for date, in the rang e 1 to 366.

mysql> SELECT DAYOFYEAR('1998-02-03');


+---------------------------------------------------------+
| DAYOFYEAR('1998-02-03') |
+---------------------------------------------------------+
| 34 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

EXTRACT(unit FROM date)


T he EXT RACT () function uses the same kinds of unit specifiers as DAT E_ADD() or DAT E_SUB(), but extracts
parts from the date rather than performing date arithmetic.

mysql> SELECT EXTRACT(YEAR FROM '1999-07-02');


+---------------------------------------------------------+
| EXTRACT(YEAR FROM '1999-07-02') |
+---------------------------------------------------------+
| 1999 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT EXTRACT(YEAR_MONTH FROM '1999-07-02 01:02:03');


+---------------------------------------------------------+
| EXTRACT(YEAR_MONTH FROM '1999-07-02 01:02:03') |
+---------------------------------------------------------+
| 199907 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
FROM_DAYS(N)
Given a day number N, returns a DAT E value.

mysql> SELECT FROM_DAYS(729669);


+---------------------------------------------------------+
| FROM_DAYS(729669) |
+---------------------------------------------------------+
| 1997-10-07 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

Use FROM_DAYS() with caution on old dates. It is not intended for use with values that precede the advent of the
Greg orian calendar (1582).

FROM_UNIXTIME(unix_timestamp)
FROM_UNIXTIME(unix_timestamp,format)
Returns a representation of the unix_timestamp arg ument as a value in 'YYYY-MM-DD HH:MM:SS' or
YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context. T he
value is expressed in the current time zone. unix_timestamp is an internal timestamp value such as is produced by
the UNIX_T IMEST AMP() function.

If format is g iven, the result is formatted according to the format string , which is used the same way as listed in
the entry for the DAT E_FORMAT () function.

mysql> SELECT FROM_UNIXTIME(875996580);


+---------------------------------------------------------+
| FROM_UNIXTIME(875996580) |
+---------------------------------------------------------+
| 1997-10-04 22:23:00 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

HOUR(time)
Returns the hour for time. T he rang e of the return value is 0 to 23 for time-of-day values. However, the rang e of
T IME values actually is much larg er, so HOUR can return values g reater than 23.

mysql> SELECT HOUR('10:05:03');


+---------------------------------------------------------+
| HOUR('10:05:03') |
+---------------------------------------------------------+
| 10 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

LAST_DAY(date)
T akes a date or datetime value and returns the corresponding value for the last day of the month. Returns NULL
if the arg ument is invalid.

mysql> SELECT LAST_DAY('2003-02-05');


+---------------------------------------------------------+
| LAST_DAY('2003-02-05') |
+---------------------------------------------------------+
| 2003-02-28 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

LOCALTIME and LOCALTIME()


LOCALT IME and LOCALT IME() are synonyms for NOW().
LOCALTIMESTAMP and LOCALTIMESTAMP()
LOCALT IMEST AMP and LOCALT IMEST AMP() are synonyms for NOW().

MAKEDATE(year,dayofyear)
Returns a date, g iven year and day-of-year values. dayofyear must be g reater than 0 or the result is NULL.

mysql> SELECT MAKEDATE(2001,31), MAKEDATE(2001,32);


+---------------------------------------------------------+
| MAKEDATE(2001,31), MAKEDATE(2001,32) |
+---------------------------------------------------------+
| '2001-01-31', '2001-02-01' |
+---------------------------------------------------------+
1 row in set (0.00 sec)

MAKETIME(hour,minute,second)
Returns a time value calculated from the hour, minute, and second arg uments.

mysql> SELECT MAKETIME(12,15,30);


+---------------------------------------------------------+
| MAKETIME(12,15,30) |
+---------------------------------------------------------+
| '12:15:30' |
+---------------------------------------------------------+
1 row in set (0.00 sec)

MICROSECOND(expr)
Returns the microseconds from the time or datetime expression expr as a number in the rang e from 0 to
999999.

mysql> SELECT MICROSECOND('12:00:00.123456');


+---------------------------------------------------------+
| MICROSECOND('12:00:00.123456') |
+---------------------------------------------------------+
| 123456 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

MINUTE(time)
Returns the minute for time, in the rang e 0 to 59.

mysql> SELECT MINUTE('98-02-03 10:05:03');


+---------------------------------------------------------+
| MINUTE('98-02-03 10:05:03') |
+---------------------------------------------------------+
| 5 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

MONTH(date)
Returns the month for date, in the rang e 0 to 12.

mysql> SELECT MONTH('1998-02-03')


+---------------------------------------------------------+
| MONTH('1998-02-03') |
+---------------------------------------------------------+
| 2 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
MONTHNAME(date)
Returns the full name of the month for date.

mysql> SELECT MONTHNAME('1998-02-05');


+---------------------------------------------------------+
| MONTHNAME('1998-02-05') |
+---------------------------------------------------------+
| February |
+---------------------------------------------------------+
1 row in set (0.00 sec)

NOW()
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format,
depending on whether the function is used in a string or numeric context. T he value is expressed in the current
time zone.

mysql> SELECT NOW();


+---------------------------------------------------------+
| NOW() |
+---------------------------------------------------------+
| 1997-12-15 23:50:26 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

PERIOD_ADD(P,N)
Adds N months to period P (in the format YYMM or YYYYMM). Returns a value in the format YYYYMM. Note
that the period arg ument P is not a date value.

mysql> SELECT PERIOD_ADD(9801,2);


+---------------------------------------------------------+
| PERIOD_ADD(9801,2) |
+---------------------------------------------------------+
| 199803 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

PERIOD_DIFF(P1,P2)
Returns the number of months between periods P1 and P2. P1 and P2 should be in the format YYMM or
YYYYMM. Note that the period arg uments P1 and P2 are not date values.

mysql> SELECT PERIOD_DIFF(9802,199703);


+---------------------------------------------------------+
| PERIOD_DIFF(9802,199703) |
+---------------------------------------------------------+
| 11 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

QUARTER(date)
Returns the quarter of the year for date, in the rang e 1 to 4.

mysql> SELECT QUARTER('98-04-01');


+---------------------------------------------------------+
| QUARTER('98-04-01') |
+---------------------------------------------------------+
| 2 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SECOND(time)
Returns the second for time, in the rang e 0 to 59.

mysql> SELECT SECOND('10:05:03');


+---------------------------------------------------------+
| SECOND('10:05:03') |
+---------------------------------------------------------+
| 3 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SEC_TO_TIME(seconds)
Returns the seconds arg ument, converted to hours, minutes and seconds, as a value in 'HH:MM:SS' or
HHMMSS format, depending on whether the function is used in a string or numeric context.

mysql> SELECT SEC_TO_TIME(2378);


+---------------------------------------------------------+
| SEC_TO_TIME(2378) |
+---------------------------------------------------------+
| 00:39:38 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

STR_TO_DATE(str,format)
T his is the inverse of the DAT E_FORMAT () function. It takes a string str and a format string format.
ST R_T O_DAT E() returns a DAT ET IME value if the format string contains both date and time parts or a DAT E
or T IME value if the string contains only date or time parts.

mysql> SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');


+---------------------------------------------------------+
| STR_TO_DATE('04/31/2004', '%m/%d/%Y') |
+---------------------------------------------------------+
| 2004-04-31 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SUBDATE(date,INTERVAL expr unit) and SUBDATE(expr,days)


When invoked with the INT ERVAL form of the second arg ument, SUBDAT E() is a synonym for DAT E_SUB().
For information on the INT ERVAL unit arg ument, see the discussion for DAT E_ADD().

mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);


+---------------------------------------------------------+
| DATE_SUB('1998-01-02', INTERVAL 31 DAY) |
+---------------------------------------------------------+
| 1997-12-02 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT SUBDATE('1998-01-02', INTERVAL 31 DAY);


+---------------------------------------------------------+
| SUBDATE('1998-01-02', INTERVAL 31 DAY) |
+---------------------------------------------------------+
| 1997-12-02 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SUBTIME(expr1,expr2)
SUBT IME() returns expr1 . expr2 expressed as a value in the same format as expr1. expr1 is a time or datetime
expression, and expr2 is a time.

mysql> SELECT SUBTIME('1997-12-31 23:59:59.999999',


-> '1 1:1:1.000002');
+---------------------------------------------------------+
| SUBTIME('1997-12-31 23:59:59.999999'... |
+---------------------------------------------------------+
| 1997-12-30 22:58:58.999997 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

SYSDATE()
Returns the current date and time as a value 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 SYSDATE();


+---------------------------------------------------------+
| SYSDATE() |
+---------------------------------------------------------+
| 2006-04-12 13:47:44 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

TIME(expr)
Extracts the time part of the time or datetime expression expr and returns it as a string .

mysql> SELECT TIME('2003-12-31 01:02:03');


+---------------------------------------------------------+
| TIME('2003-12-31 01:02:03') |
+---------------------------------------------------------+
| 01:02:03 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

TIMEDIFF(expr1,expr2)
T IMEDIFF() returns expr1 . expr2 expressed as a time value. expr1 and expr2 are time or date-and-time
expressions, but both must be of the same type.

mysql> SELECT TIMEDIFF('1997-12-31 23:59:59.000001',


-> '1997-12-30 01:01:01.000002');
+---------------------------------------------------------+
| TIMEDIFF('1997-12-31 23:59:59.000001'..... |
+---------------------------------------------------------+
| 46:58:57.999999 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

TIMESTAMP(expr), TIMESTAMP(expr1,expr2)
With a sing le arg ument, this function returns the date or datetime expression expr as a datetime value. With two
arg uments, it adds the time expression expr2 to the date or datetime expression expr1 and returns the result as
a datetime value.

mysql> SELECT TIMESTAMP('2003-12-31');


+---------------------------------------------------------+
| TIMESTAMP('2003-12-31') |
+---------------------------------------------------------+
| 2003-12-31 00:00:00 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

TIMESTAMPADD(unit,interval,datetime_expr)
Adds the integ er expression interval to the date or datetime expression datetime_expr. T he unit for interval is
g iven by the unit arg ument, which should be one of the following values: FRAC_SECOND, SECOND, MINUT E,
HOUR, DAY, WEEK, MONT H, QUART ER or YEAR.

T he unit value may be specified using one of keywords as shown or with a prefix of SQL_T SI_. For example,
DAY and SQL_T SI_DAY both are leg al.

mysql> SELECT TIMESTAMPADD(MINUTE,1,'2003-01-02');


+---------------------------------------------------------+
| TIMESTAMPADD(MINUTE,1,'2003-01-02') |
+---------------------------------------------------------+
| 2003-01-02 00:01:00 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
Returns the integ er difference between the date or datetime expressions datetime_expr1 and datetime_expr2.
T he unit for the result is g iven by the unit arg ument. T he leg al values for unit are the same as those listed in the
description of the T IMEST AMPADD() function.

mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');


+---------------------------------------------------------+
| TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01') |
+---------------------------------------------------------+
| 3 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

TIME_FORMAT(time,format)
T his is used like the DAT E_FORMAT () function, but the format string may contain format specifiers only for
hours, minutes and seconds.

If the time value contains an hour part that is g reater than 23, the %H and %k hour format specifiers produce a
value larg er than the usual rang e of 0..23. T he other hour format specifiers produce the hour value modulo 12.

mysql> SELECT TIME_FORMAT('100:00:00', '%H %k %h %I %l');


+---------------------------------------------------------+
| TIME_FORMAT('100:00:00', '%H %k %h %I %l') |
+---------------------------------------------------------+
| 100 100 04 04 4 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

TIME_TO_SEC(time)
Returns the time arg ument converted to seconds.

mysql> SELECT TIME_TO_SEC('22:23:00');


+---------------------------------------------------------+
| TIME_TO_SEC('22:23:00') |
+---------------------------------------------------------+
| 80580 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

TO_DAYS(date)
Given a date, returns a day number (the number of days since year 0).

mysql> SELECT TO_DAYS(950501);


+---------------------------------------------------------+
| TO_DAYS(950501) |
+---------------------------------------------------------+
| 728779 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
If called with no arg ument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' UT C) as an
unsig ned integ er. If UNIX_T IMEST AMP() is called with a date arg ument, it returns the value of the arg ument as
seconds since '1970-01-01 00:00:00' UT C. date may be a DAT E string , a DAT ET IME string , a T IMEST AMP,
or a number in the format YYMMDD or YYYYMMDD.

mysql> SELECT UNIX_TIMESTAMP();


+---------------------------------------------------------+
| UNIX_TIMESTAMP() |
+---------------------------------------------------------+
| 882226357 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT UNIX_TIMESTAMP('1997-10-04 22:23:00');


+---------------------------------------------------------+
| UNIX_TIMESTAMP('1997-10-04 22:23:00') |
+---------------------------------------------------------+
| 875996580 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

UTC_DATE, UTC_DATE()
Returns the current UT C date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the
function is used in a string or numeric context.

mysql> SELECT UTC_DATE(), UTC_DATE() + 0;


+---------------------------------------------------------+
| UTC_DATE(), UTC_DATE() + 0 |
+---------------------------------------------------------+
| 2003-08-14, 20030814 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

UTC_TIME, UTC_TIME()
Returns the current UT C time as a value in 'HH:MM:SS' or HHMMSS format, depending on whether the function
is used in a string or numeric context.

mysql> SELECT UTC_TIME(), UTC_TIME() + 0;


+---------------------------------------------------------+
| UTC_TIME(), UTC_TIME() + 0 |
+---------------------------------------------------------+
| 18:07:53, 180753 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

UTC_TIMESTAMP, UTC_TIMESTAMP()
Returns the current UT C date and time as a value 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 UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0;


+---------------------------------------------------------+
| UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0 |
+---------------------------------------------------------+
| 2003-08-14 18:08:04, 20030814180804 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

WEEK(date[,mode])
T his function returns the week number for date. T he two-arg ument form of WEEK() allows you to specify
whether the week starts on Sunday or Monday and whether the return value should be in the rang e from 0 to 53
or from 1 to 53. If the mode arg ument is omitted, the value of the default_week_format system variable is used
Mode First Day of week Rang e Week 1 is the first week.

0 Sunday 0-53 with a Sunday in this year

1 Monday 0-53 with more than 3 days this year

2 Sunday 1-53 with a Sunday in this year

3 Monday 1-53 with more than 3 days this year

4 Sunday 0-53 with more than 3 days this year

5 Monday 0-53 with a Monday in this year

6 Sunday 1-53 with more than 3 days this year

7 Monday 1-53 with a Monday in this year

mysql> SELECT WEEK('1998-02-20');


+---------------------------------------------------------+
| WEEK('1998-02-20') |
+---------------------------------------------------------+
| 7 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

WEEKDAY(date)
Returns the weekday index for date (0 = Monday, 1 = T uesday, . 6 = Sunday).

mysql> SELECT WEEKDAY('1998-02-03 22:23:00');


+---------------------------------------------------------+
| WEEKDAY('1998-02-03 22:23:00') |
+---------------------------------------------------------+
| 1 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

WEEKOFYEAR(date)
Returns the calendar week of the date as a number in the rang e from 1 to 53. WEEKOFYEAR() is a compatibility
function that is equivalent to WEEK(date,3).

mysql> SELECT WEEKOFYEAR('1998-02-20');


+---------------------------------------------------------+
| WEEKOFYEAR('1998-02-20') |
+---------------------------------------------------------+
| 8 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

YEAR(date)
Returns the year for date, in the rang e 1000 to 9999, or 0 for the .zero. date.

mysql> SELECT YEAR('98-02-03');


+---------------------------------------------------------+
| YEAR('98-02-03') |
+---------------------------------------------------------+
| 1998 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
YEARWEEK(date), YEARWEEK(date,mode)
Returns year and week for a date. T he mode arg ument works exactly like the mode arg ument to WEEK(). T he
year in the result may be different from the year in the date arg ument for the first and the last week of the year.

mysql> SELECT YEARWEEK('1987-01-01');


+---------------------------------------------------------+
| YEAR('98-02-03')YEARWEEK('1987-01-01') |
+---------------------------------------------------------+
| 198653 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

Note that the week number is different from what the WEEK() function would return (0) for optional arg uments 0
or 1, as WEEK() then returns the week in the context of the g iven year.

For more information, check MySQL Official Website - Date and T ime Functions

You might also like