MySQL Functions Reference

Updated · 5 min read

This page lists the MySQL functions you'll meet most, what each does, and how to write the same thing in PostgreSQL, the database this course's examples run on. It covers the functions in the common online MySQL references. Names follow the MySQL 8.4 documentation; the examples on this site don't run MySQL, so test a function on your own server before relying on it. The lessons String Functions, Numeric Functions, Date Functions and Type Conversion explain the ideas.

String Functions

MySQLWhat it doesPostgreSQL
ASCII(c)Code of the first characterascii(c)
CHAR_LENGTH(s)Number of characterslength(s)
CHARACTER_LENGTH(s)Same as CHAR_LENGTHlength(s)
CONCAT(a, b, ...)Join strings; NULL if any is NULL`a
CONCAT_WS(sep, a, b)Join with a separatorconcat_ws(sep, a, b)
FIELD(s, a, b, ...)Position of s in the listarray_position(ARRAY[a, b], s)
FIND_IN_SET(s, list)Position in a comma listarray_position(string_to_array(list, ','), s)
FORMAT(n, d)Number with thousands separatorsto_char(n, 'FM999,999.00')
INSERT(s, pos, len, new)Replace part of a stringoverlay(s PLACING new FROM pos FOR len)
INSTR(s, sub)Position of a substringposition(sub IN s)
LCASE(s) / LOWER(s)Lowercaselower(s)
LEFT(s, n)First n charactersleft(s, n)
LENGTH(s)Length in bytesoctet_length(s)
LOCATE(sub, s)Position of a substringposition(sub IN s)
LPAD(s, n, pad)Pad on the leftlpad(s, n, pad)
LTRIM(s)Remove leading spacesltrim(s)
MID(s, pos, len)Part of a stringsubstr(s, pos, len)
POSITION(sub IN s)Position of a substringposition(sub IN s)
REPEAT(s, n)Repeat a stringrepeat(s, n)
REPLACE(s, a, b)Replace textreplace(s, a, b)
REVERSE(s)Reversereverse(s)
RIGHT(s, n)Last n charactersright(s, n)
RPAD(s, n, pad)Pad on the rightrpad(s, n, pad)
RTRIM(s)Remove trailing spacesrtrim(s)
SPACE(n)n spacesrepeat(' ', n)
STRCMP(a, b)-1, 0 or 1 comparing stringsCASE comparison
SUBSTR / SUBSTRING(s, pos, len)Part of a stringsubstring(s FROM pos FOR len)
SUBSTRING_INDEX(s, delim, n)Text before the nth delimitersplit_part (one field)
TRIM(s)Remove spaces at both endstrim(s)
UCASE(s) / UPPER(s)Uppercaseupper(s)

Numeric Functions

MySQLWhat it doesPostgreSQL
ABS(x)Absolute valueabs(x)
CEIL(x) / CEILING(x)Round upceil(x)
FLOOR(x)Round downfloor(x)
ROUND(x, d)Roundround(x, d)
TRUNCATE(x, d)Cut decimalstrunc(x, d)
MOD(a, b) / a % bRemaindermod(a, b)
DIVInteger divisiondiv(a, b)
POW(x, y) / POWERPowerpower(x, y)
SQRT(x)Square rootsqrt(x)
RAND()Random number 0 to 1random()
SIGN(x)Signsign(x)
PI()πpi()
GREATEST(a, b)Largestgreatest(a, b)
LEAST(a, b)Smallestleast(a, b)
AVG / COUNT / MAX / MIN / SUMAggregatessame names
LOG / LN / LOG10 / LOG2 / EXPLogarithms and e^xlog, ln, log(10, x), log(2, x), exp
SIN / COS / TAN / ATAN / RADIANS / DEGREESTrigonometrysame names

Date Functions

MySQLWhat it doesPostgreSQL
ADDDATE / DATE_ADD(d, INTERVAL n DAY)Add to a dated + n, or d + INTERVAL 'n days'
DATE_SUB(d, INTERVAL n DAY)Subtract from a dated - n
ADDTIME(t, interval)Add a timet + interval
CURDATE() / CURRENT_DATEToday's dateCURRENT_DATE
CURTIME() / CURRENT_TIMECurrent timeCURRENT_TIME
NOW() / CURRENT_TIMESTAMPCurrent date and timenow()
DATE(dt)Date partdt::date
DATEDIFF(a, b)Days betweena - b (dates)
DATE_FORMAT(d, fmt)Format a dateto_char(d, fmt)
DAY / DAYOFMONTH(d)Day of monthEXTRACT(DAY FROM d)
DAYNAME(d)Weekday nameto_char(d, 'FMDay')
DAYOFWEEK(d)1 = SundayEXTRACT(DOW FROM d) + 1
DAYOFYEAR(d)Day of yearEXTRACT(DOY FROM d)
EXTRACT(unit FROM d)Part of a dateEXTRACT(unit FROM d)
FROM_DAYS(n)Date from a day numberno direct equivalent
HOUR / MINUTE / SECOND(t)Parts of a timeEXTRACT(HOUR FROM t) ...
LAST_DAY(d)Last day of the month(date_trunc('month', d) + INTERVAL '1 month - 1 day')::date
MAKEDATE(year, dayofyear)Build a datemake_date(year, 1, 1) + dayofyear - 1
MONTH(d) / MONTHNAME(d)Month number and nameEXTRACT(MONTH FROM d), to_char(d, 'FMMonth')
QUARTER(d)QuarterEXTRACT(QUARTER FROM d)
STR_TO_DATE(s, fmt)Parse text as a dateto_date(s, fmt)
TIMESTAMPDIFF(unit, a, b)Difference in unitsEXTRACT, age()
WEEK / WEEKOFYEAR(d)Week numberEXTRACT(WEEK FROM d)
YEAR(d)YearEXTRACT(YEAR FROM d)
YEARWEEK(d)Year and weekto_char(d, 'IYYYIW') (ISO weeks; MySQL's default mode differs)

Advanced Functions

MySQLWhat it doesPostgreSQL
BIN(n)Binary text of a numbern::bit(8)::text
CAST(x AS type) / CONVERT(x, type)Convert a typeCAST(x AS type)
COALESCE(a, b)First non-NULLCOALESCE(a, b)
CONNECTION_ID()Session idpg_backend_pid()
CURRENT_USER / USER()Current usercurrent_user
DATABASE()Current databasecurrent_database()
IF(cond, a, b)If / elseCASE WHEN cond THEN a ELSE b END
IFNULL(a, b)Replace NULLCOALESCE(a, b)
ISNULL(x)1 if NULLx IS NULL
LAST_INSERT_ID()Last generated idRETURNING id, lastval()
NULLIF(a, b)NULL when equalNULLIF(a, b)
VERSION()Server versionversion()

For the full syntax of common tasks in MySQL, see the MySQL Syntax.

Was this tutorial helpful?