iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

MySQL Functions

Quick MySQL function reference. Organised by purpose, with one-line summaries — read the MySQL docs for full signatures.

String

FunctionDoes
CONCAT(a, b, …)Join strings.
CONCAT_WS(sep, a, b)Join with a separator.
UPPER(s) / LOWER(s)Case.
SUBSTRING(s, pos, len)Substring.
LENGTH(s) / CHAR_LENGTH(s)Bytes / characters.
REPLACE(s, from, to)Replace.
TRIM(s)Trim whitespace.
LPAD / RPADPad to length.

Numeric

ABS, CEIL, FLOOR, ROUND(x, n), MOD(x, y), POWER, SQRT, RAND(), SIGN, TRUNCATE.

Date / time

FunctionReturns
NOW()Current datetime.
CURDATE() / CURTIME()Today / now-time only.
DATE(x)Strip the time part.
DATE_ADD(x, INTERVAL 7 DAY)Add an interval.
DATEDIFF(a, b)Days between.
DATE_FORMAT(x, '%Y-%m')Format to string.
UNIX_TIMESTAMP(x)Seconds since epoch.

Aggregate & conditional

COUNT, SUM, AVG, MIN, MAX, GROUP_CONCAT, IFNULL(x, y), COALESCE(a, b, …), IF(cond, t, f), CASE … END.

JSON (MySQL 5.7+)

JSON_OBJECT('k', v), JSON_ARRAY(…), JSON_EXTRACT(j, '$.k') (shortcut j->'$.k'), JSON_UNQUOTE, JSON_SET, JSON_VALID.

Tip: The MySQL docs have a search bar — for any function not listed here, type MYSQL_FUNCTION_NAME mysql docs in your favourite search engine and the official page is the first result.

Example

Example
SELECT NOW(), CURDATE(),
       UPPER('hi'), CONCAT('a','b'),
       IFNULL(phone, 'n/a')
FROM customers;
Try it Yourself »

Exercise

MySQL function returning the current datetime.

SELECT ();

Test yourself

Q1. MySQL "current datetime" is…
Q2. NULL-safe fallback in MySQL is…
Q3. JSON path extraction in MySQL is…

Discussion

Loading…