SQL - Date Functions: Name Desc Ription
SQL - Date Functions: Name Desc Ription
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.
LAST _DAY Returns the last day of the month for the arg ument
SUBDAT E() When invoked with three arg uments a synonym for DAT E_SUB()
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
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.
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.
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.
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.
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.
DATE(expr)
Extracts the date part of the date or datetime expression expr.
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.
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;
MICROSECOND MICROSECONDS
SECOND SECONDS
MINUT E MINUT ES
HOUR HOURS
DAY DAYS
WEEK WEEKS
MONT H MONT HS
YEAR YEARS
SECOND_MICROSECOND 'SECONDS.MICROSECONDS'
HOUR_MICROSECOND 'HOURS.MICROSECONDS'
DAY_MICROSECOND 'DAYS.MICROSECONDS'
T he values QUART ER and WEEK are available beg inning with MySQL 5.0.0.
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.
%D Day of the month with Eng lish suffix (0th, 1st, 2nd, 3rd, .)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%k Hour (0..23)
%l Hour (1..12)
%S Seconds (00..59)
%s Seconds (00..59)
%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
%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
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.
DAYOFWEEK(date)
Returns the weekday index for date (1 = Sunday, 2 = Monday, ., 7 = Saturday). T hese index values correspond to
the ODBC standard.
DAYOFYEAR(date)
Returns the day of the year for date, in the rang e 1 to 366.
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.
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.
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.
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.
MAKETIME(hour,minute,second)
Returns a time value calculated from the hour, minute, and second arg uments.
MICROSECOND(expr)
Returns the microseconds from the time or datetime expression expr as a number in the rang e from 0 to
999999.
MINUTE(time)
Returns the minute for time, in the rang e 0 to 59.
MONTH(date)
Returns the month for date, in the rang e 0 to 12.
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.
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.
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.
QUARTER(date)
Returns the quarter of the year for date, in the rang e 1 to 4.
SECOND(time)
Returns the second for time, in the rang e 0 to 59.
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.
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.
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.
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.
TIME(expr)
Extracts the time part of the time or datetime expression expr and returns it as a string .
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.
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.
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.
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.
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.
TIME_TO_SEC(time)
Returns the time arg ument converted to seconds.
TO_DAYS(date)
Given a date, returns a day number (the number of days since year 0).
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.
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.
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.
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.
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.
WEEKDAY(date)
Returns the weekday index for date (0 = Monday, 1 = T uesday, . 6 = Sunday).
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).
YEAR(date)
Returns the year for date, in the rang e 1000 to 9999, or 0 for the .zero. date.
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