Example
Return the year part of a date:
SELECT YEAR("2017-06-15");
Definition and Usage
The YEAR() function returns the year part for a given date (a number from 1000 to 9999).
Syntax
YEAR(date)
Return the year part of a date:
SELECT YEAR("2017-06-15");
The YEAR() function returns the year part for a given date (a number from 1000 to 9999).
YEAR(date)
Return the week number for a date:
SELECT WEEKOFYEAR("2017-06-15");
The WEEKOFYEAR() function returns the week number for a given date (a number from 1 to 53).
Note: This function assumes that the first day of the week is Monday and the first week of the year has more than 3 days.
WEEKOFYEAR(date)
Return the week number for a date:
SELECT WEEK("2017-06-15");
The WEEK() function returns the week number for a given date (a number from 0 to 53).
WEEK(date, firstdayofweek)
Return the number of days between the date and year 0:
SELECT TO_DAYS("2017-06-20");
The TO_DAYS() function returns the number of days between a date and year 0 (date “0000-00-00”).
The TO_DAYS() function can be used only with dates within the Gregorian calendar.
TO_DAYS(date)
Return a datetime value based on the arguments:
SELECT TIMESTAMP("2017-07-23", "13:10:11");
The TIMESTAMP() function returns a datetime value based on a date or datetime value.
Note: If there are specified two arguments with this function, it first adds the second argument to the first, and then returns a datetime value.
TIMESTAMP(expression, time)
Return the difference between two time expressions:
SELECT TIMEDIFF("13:10:11", "13:10:10");
The TIMEDIFF() function returns the difference between two time/datetime expressions.
Note: time1 and time2 should be in the same format, and the calculation is time1 – time2.
TIMEDIFF(time1, time2)
Convert a time value into seconds:
SELECT TIME_TO_SEC("19:30:10");
The TIME_TO_SEC() function converts a time value into seconds.
TIME_TO_SEC(time)
Format a time:
SELECT TIME_FORMAT("19:30:10", "%H %i %s");
The TIME_FORMAT() function formats a time by a specified format.
TIME_FORMAT(time, format)
Extract the time part from a time expression:
SELECT TIME("19:30:10");
The TIME() function extracts the time part from a given time/datetime.
Note: This function returns “00:00:00” if expression is not a datetime/time, or NULL if expression is NULL.
TIME(expression)
Return the current date and time:
SELECT SYSDATE();
The SYSDATE() function returns the current date and time.
Note: The date and time is returned as “YYYY-MM-DD HH:MM:SS” (string) or as YYYYMMDDHHMMSS (numeric).
SYSDATE()