SQL reference (MySQL and MariaDB dialects)

Date and time functions

The date and time functions of Raft DB: current time, parts of a date, arithmetic with intervals, formatting and parsing.

Dates and times are kept as text in the form MySQL writes them (YYYY-MM-DD, HH:MM:SS, YYYY-MM-DD HH:MM:SS[.ffffff]), and the date functions read that text. A value that is not a date, and NULL, give NULL.

What to know before using them:

  • Time zone. Raft DB runs in UTC. NOW(), CURDATE(), UNIX_TIMESTAMP() and the others use UTC whatever time_zone says, and CONVERT_TZ does not convert. The comparison servers were in another zone, so these functions are not compared with them.
  • Whole seconds. The functions below compute with whole seconds. A DATETIME(6) column keeps its microseconds (see Date and time types), but MICROSECOND() and %f do not read them.
  • Interval units. INTERVAL expr unit accepts MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER and YEAR. date + INTERVAL 1 DAY and date - INTERVAL 1 DAY work as operators.
  • Zero dates. 0000-00-00 is accepted when the sql_mode does not have NO_ZERO_DATE (see SQL modes); YEAR('0000-00-00 00:00:00') is NULL.

Summary of functions#

FunctionWhat it doesDialect
ADDDATEAdds an interval, or a number of days, to date.MySQL and MariaDB
ADDTIMEAdds the time expr2 to the time or datetime expr1.MySQL and MariaDB
ADD_MONTHSAdds n months to date.MariaDB function; Raft DB also accepts it when it runs as MySQL
CONVERT_TZAccepted so that applications run, but it does not convert: it returns dt unchanged.MySQL and MariaDB
CURDATEReturns the current date as YYYY-MM-DD, in UTC.MySQL and MariaDB
CURTIMEReturns the current time as HH:MM:SS, in UTC.MySQL and MariaDB
DATEReturns the date part of a date or datetime.MySQL and MariaDB
DATEDIFFReturns expr1 - expr2 in days; only the date parts are used.MySQL and MariaDB
DATE_ADDAdds an interval to a date or datetime.MySQL and MariaDB
DATE_FORMATWrites date as the format says.MySQL and MariaDB
DATE_SUBSubtracts an interval from a date or datetime.MySQL and MariaDB
DAYReturns the day of the month, 1 to 31.MySQL and MariaDB
DAYNAMEReturns the name of the weekday, in English.MySQL and MariaDB
DAYOFWEEKReturns the weekday index: 1 is Sunday, 7 is Saturday.MySQL and MariaDB
DAYOFYEARReturns the day of the year, 1 to 366.MySQL and MariaDB
EXTRACTReturns one part of date.MySQL and MariaDB
FROM_DAYSReturns the date that is n days from year 0.MySQL and MariaDB
FROM_UNIXTIMETurns a count of seconds after 1970-01-01 00:00:00 UTC into a datetime, given as text, or formatted with format the way DATE_FORMAT does it.MySQL and MariaDB
HOURReturns the hour of a time or datetime.MySQL and MariaDB
LAST_DAYReturns the last day of the month of date.MySQL and MariaDB
LOCALTIMESynonyms of NOW.MySQL and MariaDB
MAKEDATEReturns the date for a year and a day of the year.MySQL and MariaDB
MAKETIMEReturns the time made of the three parts.MySQL and MariaDB
MICROSECONDAccepted; always returns 0, because the date functions work with whole seconds.MySQL and MariaDB
MINUTEReturns the minute of a time or datetime, 0 to 59.MySQL and MariaDB
MONTHReturns the month of date, 1 to 12.MySQL and MariaDB
MONTHNAMEReturns the name of the month, in English.MySQL and MariaDB
NOWReturns the current date and time as YYYY-MM-DD HH:MM:SS, in UTC, with fsp (0 to 6) fractional digits.MySQL and MariaDB
PERIOD_ADDAdds n months to a period written YYMM or YYYYMM; returns YYYYMM.MySQL and MariaDB
PERIOD_DIFFReturns the number of months between two periods written YYMM or YYYYMM.MySQL and MariaDB
QUARTERReturns the quarter of the year, 1 to 4.MySQL and MariaDB
SECONDReturns the second of a time or datetime, 0 to 59.MySQL and MariaDB
SEC_TO_TIMEReturns a number of seconds as HH:MM:SS.MySQL and MariaDB
STR_TO_DATEReads str as a date or datetime written the way format says (the specifiers %Y %y %m %c %d %e %H %k %h %I %l %i %s %S %p %M %b %%).MySQL and MariaDB
SUBDATESubtracts an interval, or a number of days, from date.MySQL and MariaDB
SUBTIMESubtracts the time expr2 from the time or datetime expr1.MySQL and MariaDB
TIMEReturns the time part of a datetime.MySQL and MariaDB
TIMEDIFFReturns expr1 - expr2 as a time.MySQL and MariaDB
TIMESTAMPReturns expr as a datetime (a date gets the time 00:00:00).MySQL and MariaDB
TIMESTAMPADDAdds interval units to datetime.MySQL and MariaDB
TIMESTAMPDIFFReturns datetime2 - datetime1 in whole units (SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR).MySQL and MariaDB
TIME_FORMATWrites a time as format says (%H %k %h %I %l %i %s %S %p %r %T %f %%).MySQL and MariaDB
TIME_TO_SECReturns a time as a number of seconds.MySQL and MariaDB
TO_DAYSReturns the number of days from year 0 to date.MySQL and MariaDB
TO_SECONDSReturns the number of seconds from year 0 to a date or datetime.MySQL and MariaDB
UNIX_TIMESTAMPWithout an argument returns the seconds since 1970-01-01 00:00:00 UTC now; with one, the seconds of that date (taken as UTC).MySQL and MariaDB
UTC_DATEReturns the current UTC date.MySQL and MariaDB
UTC_TIMEReturns the current UTC time.MySQL and MariaDB
UTC_TIMESTAMPReturns the current UTC date and time.MySQL and MariaDB
WEEKReturns the week of the year, 0 to 53, counted as mode 0 does (weeks start on Sunday; week 1 is the first with a Sunday in the year).MySQL and MariaDB
WEEKDAYReturns the weekday index: 0 is Monday, 6 is Sunday.MySQL and MariaDB
WEEKOFYEARReturns the ISO calendar week, 1 to 53.MySQL and MariaDB
YEARReturns the year of date.MySQL and MariaDB
YEARWEEKReturns the year and week (YYYYWW) as WEEK mode 0 counts them.MySQL and MariaDB

ADDDATE#

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

Adds an interval, or a number of days, to date. Synonym of DATE_ADD for the first form.

Example:

SQL
SELECT ADDDATE('2024-01-31', 1);

Result: 2024-02-01

ADDTIME#

SQL
ADDTIME(expr1, expr2)

Adds the time expr2 to the time or datetime expr1.

Example:

SQL
SELECT ADDTIME('01:00:00', '02:30:00');

Result: 03:30:00

ADD_MONTHS#

MariaDB function; Raft DB also accepts it when it runs as MySQL.

SQL
ADD_MONTHS(date, n)

Adds n months to date. The last day of a month stays the last day of the month it moves to.

Example:

SQL
SELECT ADD_MONTHS('2012-01-31', 1);

Result: 2012-02-29

CONVERT_TZ#

SQL
CONVERT_TZ(dt, from_tz, to_tz)

Accepted so that applications run, but it does not convert: it returns dt unchanged. Raft DB runs in UTC and has no time zone tables.

Example:

SQL
SELECT CONVERT_TZ('2024-01-01 12:00:00', '+00:00', '+05:30');

Result: 2024-01-01 12:00:00 (MySQL gives 2024-01-01 17:30:00)

CURDATE#

SQL
CURDATE()
CURRENT_DATE
CURRENT_DATE()

Returns the current date as YYYY-MM-DD, in UTC.

Example:

SQL
SELECT CURDATE();

Result: a date such as 2026-11-12

CURTIME#

SQL
CURTIME()
CURRENT_TIME
CURRENT_TIME()

Returns the current time as HH:MM:SS, in UTC.

Example:

SQL
SELECT CURTIME();

Result: a time such as 14:05:09

DATE#

SQL
DATE(expr)

Returns the date part of a date or datetime.

Example:

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

Result: 2003-12-31

DATEDIFF#

SQL
DATEDIFF(expr1, expr2)

Returns expr1 - expr2 in days; only the date parts are used.

Example:

SQL
SELECT DATEDIFF('2024-03-01', '2024-02-01');

Result: 29

DATE_ADD#

SQL
DATE_ADD(date, INTERVAL expr unit)
date + INTERVAL expr unit

Adds an interval to a date or datetime. The units are MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER and YEAR; the compound units (DAY_HOUR, YEAR_MONTH, ...) are not supported. If the day does not exist in the new month, the last day of that month is used. The result has a time part if date has one or the unit is a time unit.

Example:

SQL
SELECT DATE_ADD('2024-01-31 10:00:00', INTERVAL 1 MONTH);

Result: 2024-02-29 10:00:00

DATE_FORMAT#

SQL
DATE_FORMAT(date, format)

Writes date as the format says. Supported specifiers: %Y %y %m %c %d %e %H %k %h %I %l %i %s %S %p %r %T %M %b %W %a %w %j %D %%. %f always writes 000000: fractions of a second are not kept by the date functions. Other specifiers (%U, %u, %V, %v, %X, %x) are written as the letter.

Example:

SQL
SELECT DATE_FORMAT('2024-05-06 17:08:09', '%W %M %e, %Y %l:%i %p %j');

Result: Monday May 6, 2024 5:08 PM 127

DATE_SUB#

SQL
DATE_SUB(date, INTERVAL expr unit)
date - INTERVAL expr unit

Subtracts an interval from a date or datetime. Units as for DATE_ADD.

Example:

SQL
SELECT DATE_SUB('2024-03-01', INTERVAL 1 DAY);

Result: 2024-02-29

DAY#

SQL
DAY(date)
DAYOFMONTH(date)

Returns the day of the month, 1 to 31. DAYOFMONTH is a synonym.

Example:

SQL
SELECT DAY('2024-05-06');

Result: 6

DAYNAME#

SQL
DAYNAME(date)

Returns the name of the weekday, in English.

Example:

SQL
SELECT DAYNAME('2024-05-06');

Result: Monday

DAYOFWEEK#

SQL
DAYOFWEEK(date)

Returns the weekday index: 1 is Sunday, 7 is Saturday.

Example:

SQL
SELECT DAYOFWEEK('2024-05-06');

Result: 2

DAYOFYEAR#

SQL
DAYOFYEAR(date)

Returns the day of the year, 1 to 366.

Example:

SQL
SELECT DAYOFYEAR('2024-05-06');

Result: 127

EXTRACT#

SQL
EXTRACT(unit FROM date)

Returns one part of date. The units that are one word (YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, QUARTER, WEEK, MICROSECOND) are supported; the compound units (YEAR_MONTH, DAY_HOUR, ...) are not.

Example:

SQL
SELECT EXTRACT(YEAR FROM '2019-07-02');

Result: 2019

FROM_DAYS#

SQL
FROM_DAYS(n)

Returns the date that is n days from year 0. Days up to 365 give NULL in MySQL and 0000-00-00 in MariaDB.

Example:

SQL
SELECT FROM_DAYS(730669);

Result: 2000-07-03

FROM_UNIXTIME#

SQL
FROM_UNIXTIME(unix_timestamp [, format])

Turns a count of seconds after 1970-01-01 00:00:00 UTC into a datetime, given as text, or formatted with format the way DATE_FORMAT does it.

Example:

SQL
SELECT FROM_UNIXTIME(86400);

Result: 1970-01-02 00:00:00

HOUR#

SQL
HOUR(time)

Returns the hour of a time or datetime.

Example:

SQL
SELECT HOUR('2024-05-06 10:05:03');

Result: 10

LAST_DAY#

SQL
LAST_DAY(date)

Returns the last day of the month of date.

Example:

SQL
SELECT LAST_DAY('2024-02-10');

Result: 2024-02-29

LOCALTIME#

SQL
LOCALTIME [(fsp)]
LOCALTIMESTAMP [(fsp)]

Synonyms of NOW.

Example:

SQL
SELECT LOCALTIME;

Result: the current datetime

MAKEDATE#

SQL
MAKEDATE(year, dayofyear)

Returns the date for a year and a day of the year. NULL if the day is 0 or less.

Example:

SQL
SELECT MAKEDATE(2011, 31);

Result: 2011-01-31

MAKETIME#

SQL
MAKETIME(hour, minute, second)

Returns the time made of the three parts.

Example:

SQL
SELECT MAKETIME(12, 15, 30);

Result: 12:15:30

MICROSECOND#

SQL
MICROSECOND(expr)

Accepted; always returns 0, because the date functions work with whole seconds.

Example:

SQL
SELECT MICROSECOND('2024-05-06 10:05:03.123456');

Result: 0 (MySQL gives 123456)

MINUTE#

SQL
MINUTE(time)

Returns the minute of a time or datetime, 0 to 59.

Example:

SQL
SELECT MINUTE('2024-05-06 10:05:03');

Result: 5

MONTH#

SQL
MONTH(date)

Returns the month of date, 1 to 12.

Example:

SQL
SELECT MONTH('2024-05-06');

Result: 5

MONTHNAME#

SQL
MONTHNAME(date)

Returns the name of the month, in English.

Example:

SQL
SELECT MONTHNAME('2024-05-06');

Result: May

NOW#

SQL
NOW([fsp])
CURRENT_TIMESTAMP [(fsp)]
SYSDATE([fsp])

Returns the current date and time as YYYY-MM-DD HH:MM:SS, in UTC, with fsp (0 to 6) fractional digits. CURRENT_TIMESTAMP and SYSDATE are synonyms; SYSDATE is not evaluated at the time it runs, but, like NOW, once for each row.

Example:

SQL
SELECT NOW(3) LIKE '____-__-__ __:__:__.___';

Result: 1

PERIOD_ADD#

SQL
PERIOD_ADD(period, n)

Adds n months to a period written YYMM or YYYYMM; returns YYYYMM.

Example:

SQL
SELECT PERIOD_ADD(200801, 2);

Result: 200803

PERIOD_DIFF#

SQL
PERIOD_DIFF(p1, p2)

Returns the number of months between two periods written YYMM or YYYYMM.

Example:

SQL
SELECT PERIOD_DIFF(200802, 200703);

Result: 11

QUARTER#

SQL
QUARTER(date)

Returns the quarter of the year, 1 to 4.

Example:

SQL
SELECT QUARTER('2024-05-06');

Result: 2

SECOND#

SQL
SECOND(time)

Returns the second of a time or datetime, 0 to 59.

Example:

SQL
SELECT SECOND('2024-05-06 10:05:03');

Result: 3

SEC_TO_TIME#

SQL
SEC_TO_TIME(seconds)

Returns a number of seconds as HH:MM:SS.

Example:

SQL
SELECT SEC_TO_TIME(3661);

Result: 01:01:01

STR_TO_DATE#

SQL
STR_TO_DATE(str, format)

Reads str as a date or datetime written the way format says (the specifiers %Y %y %m %c %d %e %H %k %h %I %l %i %s %S %p %M %b %%). The result is a datetime when the format has an hour, minute or second, a date otherwise. NULL if str does not fit the format.

Example:

SQL
SELECT STR_TO_DATE('06/05/2024 17:08', '%d/%m/%Y %H:%i');

Result: 2024-05-06 17:08:00

SUBDATE#

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

Subtracts an interval, or a number of days, from date. Synonym of DATE_SUB for the first form.

Example:

SQL
SELECT SUBDATE('2024-03-01', 1);

Result: 2024-02-29

SUBTIME#

SQL
SUBTIME(expr1, expr2)

Subtracts the time expr2 from the time or datetime expr1.

Example:

SQL
SELECT SUBTIME('01:00:00', '02:30:00');

Result: -01:30:00

TIME#

SQL
TIME(expr)

Returns the time part of a datetime.

Example:

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

Result: 01:02:03

TIMEDIFF#

SQL
TIMEDIFF(expr1, expr2)

Returns expr1 - expr2 as a time.

Example:

SQL
SELECT TIMEDIFF('12:00:00', '10:30:00');

Result: 01:30:00

TIMESTAMP#

SQL
TIMESTAMP(expr)

Returns expr as a datetime (a date gets the time 00:00:00). The two-argument form TIMESTAMP(expr, time) is accepted and the second argument is ignored.

Example:

SQL
SELECT TIMESTAMP('2003-12-31');

Result: 2003-12-31 00:00:00

TIMESTAMPADD#

SQL
TIMESTAMPADD(unit, interval, datetime)

Adds interval units to datetime. Units as for DATE_ADD.

Example:

SQL
SELECT TIMESTAMPADD(MINUTE, 15, '2024-05-06 10:00:00');

Result: 2024-05-06 10:15:00

TIMESTAMPDIFF#

SQL
TIMESTAMPDIFF(unit, datetime1, datetime2)

Returns datetime2 - datetime1 in whole units (SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR).

Example:

SQL
SELECT TIMESTAMPDIFF(MONTH, '2024-01-31', '2024-03-30');

Result: 1

TIME_FORMAT#

SQL
TIME_FORMAT(time, format)

Writes a time as format says (%H %k %h %I %l %i %s %S %p %r %T %f %%). Hours may be more than 23.

Example:

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

Result: 100 100 04 04 4

TIME_TO_SEC#

SQL
TIME_TO_SEC(time)

Returns a time as a number of seconds.

Example:

SQL
SELECT TIME_TO_SEC('01:01:01');

Result: 3661

TO_DAYS#

SQL
TO_DAYS(date)

Returns the number of days from year 0 to date.

Example:

SQL
SELECT TO_DAYS('2008-10-07');

Result: 733687

TO_SECONDS#

SQL
TO_SECONDS(expr)

Returns the number of seconds from year 0 to a date or datetime.

Example:

SQL
SELECT TO_SECONDS('2009-11-29');

Result: 63426672000

UNIX_TIMESTAMP#

SQL
UNIX_TIMESTAMP([date])

Without an argument returns the seconds since 1970-01-01 00:00:00 UTC now; with one, the seconds of that date (taken as UTC). Returns 0 for a value that is not a date.

Example:

SQL
SELECT UNIX_TIMESTAMP('1970-01-02 00:00:00');

Result: 86400

UTC_DATE#

SQL
UTC_DATE()

Returns the current UTC date. The same as CURDATE(), as Raft DB runs in UTC.

Example:

SQL
SELECT UTC_DATE();

Result: a date such as 2026-11-12

UTC_TIME#

SQL
UTC_TIME()

Returns the current UTC time. The same as CURTIME().

Example:

SQL
SELECT UTC_TIME();

Result: a time such as 14:05:09

UTC_TIMESTAMP#

SQL
UTC_TIMESTAMP()

Returns the current UTC date and time. The same as NOW().

Example:

SQL
SELECT UTC_TIMESTAMP();

Result: a datetime

WEEK#

SQL
WEEK(date [, mode])

Returns the week of the year, 0 to 53, counted as mode 0 does (weeks start on Sunday; week 1 is the first with a Sunday in the year). The mode argument is accepted and ignored.

Example:

SQL
SELECT WEEK('2008-02-20');

Result: 7

WEEKDAY#

SQL
WEEKDAY(date)

Returns the weekday index: 0 is Monday, 6 is Sunday.

Example:

SQL
SELECT WEEKDAY('2024-05-06');

Result: 0

WEEKOFYEAR#

SQL
WEEKOFYEAR(date)

Returns the ISO calendar week, 1 to 53.

Example:

SQL
SELECT WEEKOFYEAR('2008-02-20');

Result: 8

YEAR#

SQL
YEAR(date)

Returns the year of date. NULL for the zero date 0000-00-00.

Example:

SQL
SELECT YEAR('2024-05-06 07:08:09');

Result: 2024

YEARWEEK#

SQL
YEARWEEK(date [, mode])

Returns the year and week (YYYYWW) as WEEK mode 0 counts them. The mode argument is accepted and ignored.

Example:

SQL
SELECT YEARWEEK('1987-01-01');

Result: 198652

See also#