Skip to main content

MySQL Functions

This document introduces common MySQL functions and operators by category: numeric functions, string functions, datetime functions, aggregate functions, and flow-control functions. For more function usage, see the official MySQL documentation.

MySQL Numeric Functions

Function NamePurpose
ABSReturns the absolute value.
SQRTReturns the square root.
MODReturns the remainder.
CEIL and CEILINGThese two functions have the same behavior. They return the smallest integer not less than the argument, meaning round up.
FLOORRounds down and converts the return value to a BIGINT.
RANDGenerates a random number between 0 and 1. When an integer parameter is passed, it is used to generate a repeatable sequence.
ROUNDRounds the passed parameter.
SIGNReturns the sign of the parameter.
POW and POWERThese two functions have the same behavior. They return the result of raising the parameter to a power.
SINReturns the sine value.
ASINReturns the arcsine value. It is the inverse of SIN.
COSReturns the cosine value.
ACOSReturns the arccosine value. It is the inverse of COS.
TANReturns the tangent value.
ATANReturns the arctangent value. It is the inverse of TAN.
COTReturns the cotangent value.

MySQL String Functions

Function NamePurpose
LENGTHCalculates string length and returns the byte length of the string.
CONCATConcatenates strings. The returned result is the string produced by joining one or more parameters.
INSERTReplaces a string.
LOWERConverts letters in a string to lowercase.
UPPERConverts letters in a string to uppercase.
LEFTExtracts characters from the left side of a string and returns several characters from the left.
RIGHTExtracts characters from the right side of a string and returns several characters from the right.
TRIMRemoves spaces from both sides of a string.
REPLACEReplaces strings and returns the new string after replacement.
SUBSTRINGExtracts a string and returns characters of the specified length starting from the specified position.
REVERSEReverses a string and returns a string in the opposite order from the original string.

MySQL Date and Time Functions

Function NamePurpose
CURDATE and CURRENT_DATEThese two functions have the same behavior and return the current system date.
CURTIME and CURRENT_TIMEThese two functions have the same behavior and return the current system time.
NOW and SYSDATEThese two functions have the same behavior and return the current system date and time.
UNIX_TIMESTAMPGets the UNIX timestamp and returns an unsigned integer based on the UNIX timestamp.
FROM_UNIXTIMEConverts a UNIX timestamp to a time format. It is the inverse of UNIX_TIMESTAMP.
MONTHGets the month from a specified date.
MONTHNAMEGets the English month name from a specified date.
DAYNAMEGets the English weekday name corresponding to a specified date.
DAYOFWEEKGets the index position value of the specified date in a week.
WEEKGets which week of the year a specified date belongs to. The return value range may be 0-52 or 1-53.
DAYOFYEARGets which day of the year a specified date is. The return value range is 1-366.
DAYOFMONTHGets which day of the month a specified date is. The return value range is 1-31.
YEARGets the year. The return value range is 1970-2069.
TIME_TO_SECConverts a time parameter to seconds.
SEC_TO_TIMEConverts seconds to time. It is the inverse of TIME_TO_SEC.
DATE_ADD and ADDDATEThese two functions have the same behavior and add a specified time interval to a date.
DATE_SUB and SUBDATEThese two functions have the same behavior and subtract a specified time interval from a date.
ADDTIMETime addition. Adds a specified time to the original time.
SUBTIMETime subtraction. Subtracts a specified time from the original time.
DATEDIFFGets the interval between two dates and returns parameter 1 minus parameter 2.
DATE_FORMATFormats a specified date and returns a value in the specified format based on the parameter.
WEEKDAYGets the weekday index corresponding to a specified date.

MySQL Aggregate Functions

Function NamePurpose
MAXQueries the maximum value of a specified column.
MINQueries the minimum value of a specified column.
COUNTCounts the number of rows in the query result.
SUMReturns the sum of a specified column.
AVGReturns the average value of data in a specified column.

MySQL Flow-Control Functions

Function NamePurpose
IFJudgment and flow control.
IFNULLChecks whether a value is null.
CASESearch statement.