☰ SQL lessons Home › Tutorials › SQL › MySQL Functions Reference MySQL Functions Reference 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.
Moving queries from MySQL to PostgreSQL? Watch the functions whose behavior differs, not just their names: CONCAT returns NULL if any argument is NULL in MySQL but skips it in PostgreSQL, and LENGTH counts bytes in MySQL but characters in PostgreSQL.
String Functions MySQL What it does PostgreSQL ASCII(c)Code of the first character ascii(c)CHAR_LENGTH(s)Number of characters length(s)CHARACTER_LENGTH(s)Same as CHAR_LENGTH length(s)CONCAT(a, b, ...)Join strings; NULL if any is NULL `a CONCAT_WS(sep, a, b)Join with a separator concat_ws(sep, a, b)FIELD(s, a, b, ...)Position of s in the list array_position(ARRAY[a, b], s)FIND_IN_SET(s, list)Position in a comma list array_position(string_to_array(list, ','), s)FORMAT(n, d)Number with thousands separators to_char(n, 'FM999,999.00')INSERT(s, pos, len, new)Replace part of a string overlay(s PLACING new FROM pos FOR len)INSTR(s, sub)Position of a substring position(sub IN s)LCASE(s) / LOWER(s)Lowercase lower(s)LEFT(s, n)First n characters left(s, n)LENGTH(s)Length in bytes octet_length(s)LOCATE(sub, s)Position of a substring position(sub IN s)LPAD(s, n, pad)Pad on the left lpad(s, n, pad)LTRIM(s)Remove leading spaces ltrim(s)MID(s, pos, len)Part of a string substr(s, pos, len)POSITION(sub IN s)Position of a substring position(sub IN s)REPEAT(s, n)Repeat a string repeat(s, n)REPLACE(s, a, b)Replace text replace(s, a, b)REVERSE(s)Reverse reverse(s)RIGHT(s, n)Last n characters right(s, n)RPAD(s, n, pad)Pad on the right rpad(s, n, pad)RTRIM(s)Remove trailing spaces rtrim(s)SPACE(n)n spaces repeat(' ', n)STRCMP(a, b)-1, 0 or 1 comparing strings CASE comparisonSUBSTR / SUBSTRING(s, pos, len)Part of a string substring(s FROM pos FOR len)SUBSTRING_INDEX(s, delim, n)Text before the nth delimiter split_part (one field)TRIM(s)Remove spaces at both ends trim(s)UCASE(s) / UPPER(s)Uppercase upper(s)
Numeric Functions MySQL What it does PostgreSQL ABS(x)Absolute value abs(x)CEIL(x) / CEILING(x)Round up ceil(x)FLOOR(x)Round down floor(x)ROUND(x, d)Round round(x, d)TRUNCATE(x, d)Cut decimals trunc(x, d)MOD(a, b) / a % bRemainder mod(a, b)DIVInteger division div(a, b)POW(x, y) / POWERPower power(x, y)SQRT(x)Square root sqrt(x)RAND()Random number 0 to 1 random()SIGN(x)Sign sign(x)PI()π pi()GREATEST(a, b)Largest greatest(a, b)LEAST(a, b)Smallest least(a, b)AVG / COUNT / MAX / MIN / SUMAggregates same names LOG / LN / LOG10 / LOG2 / EXPLogarithms and e^x log, ln, log(10, x), log(2, x), expSIN / COS / TAN / ATAN / RADIANS / DEGREESTrigonometry same names
Date Functions MySQL What it does PostgreSQL ADDDATE / DATE_ADD(d, INTERVAL n DAY)Add to a date d + n, or d + INTERVAL 'n days'DATE_SUB(d, INTERVAL n DAY)Subtract from a date d - nADDTIME(t, interval)Add a time t + intervalCURDATE() / CURRENT_DATEToday's date CURRENT_DATECURTIME() / CURRENT_TIMECurrent time CURRENT_TIMENOW() / CURRENT_TIMESTAMPCurrent date and time now()DATE(dt)Date part dt::dateDATEDIFF(a, b)Days between a - b (dates)DATE_FORMAT(d, fmt)Format a date to_char(d, fmt)DAY / DAYOFMONTH(d)Day of month EXTRACT(DAY FROM d)DAYNAME(d)Weekday name to_char(d, 'FMDay')DAYOFWEEK(d)1 = Sunday EXTRACT(DOW FROM d) + 1DAYOFYEAR(d)Day of year EXTRACT(DOY FROM d)EXTRACT(unit FROM d)Part of a date EXTRACT(unit FROM d)FROM_DAYS(n)Date from a day number no direct equivalent HOUR / MINUTE / SECOND(t)Parts of a time EXTRACT(HOUR FROM t) ...LAST_DAY(d)Last day of the month (date_trunc('month', d) + INTERVAL '1 month - 1 day')::dateMAKEDATE(year, dayofyear)Build a date make_date(year, 1, 1) + dayofyear - 1MONTH(d) / MONTHNAME(d)Month number and name EXTRACT(MONTH FROM d), to_char(d, 'FMMonth')QUARTER(d)Quarter EXTRACT(QUARTER FROM d)STR_TO_DATE(s, fmt)Parse text as a date to_date(s, fmt)TIMESTAMPDIFF(unit, a, b)Difference in units EXTRACT, age()WEEK / WEEKOFYEAR(d)Week number EXTRACT(WEEK FROM d)YEAR(d)Year EXTRACT(YEAR FROM d)YEARWEEK(d)Year and week to_char(d, 'IYYYIW') (ISO weeks; MySQL's default mode differs)
Advanced Functions MySQL What it does PostgreSQL BIN(n)Binary text of a number n::bit(8)::textCAST(x AS type) / CONVERT(x, type)Convert a type CAST(x AS type)COALESCE(a, b)First non-NULL COALESCE(a, b)CONNECTION_ID()Session id pg_backend_pid()CURRENT_USER / USER()Current user current_userDATABASE()Current database current_database()IF(cond, a, b)If / else CASE WHEN cond THEN a ELSE b ENDIFNULL(a, b)Replace NULL COALESCE(a, b)ISNULL(x)1 if NULL x IS NULLLAST_INSERT_ID()Last generated id RETURNING id, lastval()NULLIF(a, b)NULL when equal NULLIF(a, b)VERSION()Server version version()
For the full syntax of common tasks in MySQL, see the MySQL Syntax .
✕ Was this tutorial helpful?
👍 Yes 👎 No