MySQL Functions, JSON, and Date Cheatsheet

now, date_add, date_sub, date_format, timestampdiff, json_extract, json_set, json_object, concat, coalesce, and case in MySQL.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

date and time functions

The most useful date arithmetic and formatting helpers.

Get the current date and time

Return current timestamp values.

sqlANYnowcurrent_datetime
sql
SELECT NOW(), CURRENT_DATE(), CURRENT_TIME();

Useful for diagnostics, defaults, and test queries.

Add an interval to a date

Calculate a future date or time.

sqlANYdate_addinterval
sql
SELECT DATE_ADD(NOW(), INTERVAL 7 DAY) AS one_week_from_now;

Common in expiration, scheduling, and retention logic.

Subtract an interval

Calculate a relative date in the past.

sqlANYdate_subinterval
sql
SELECT DATE_SUB(NOW(), INTERVAL 30 DAY) AS thirty_days_ago;

Handy for rolling windows and recent-activity filters.

Format dates for display

Render a datetime using format specifiers.

sqlANYdate_formatformatting
sql
SELECT DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') AS created_at_fmt FROM orders LIMIT 5;

Formatting is often better done in the app layer, but this is useful in reports and exports.

Compute the difference between two timestamps

Calculate age or duration.

sqlANYtimestampdiffduration
sql
SELECT TIMESTAMPDIFF(MINUTE, created_at, NOW()) AS age_minutes FROM sessions;

Useful for SLAs, queue age, and freshness calculations.

json and string functions

Practical JSON and text-processing examples for application data.

Extract a value from JSON

Read a path inside a JSON document.

sqlANYjson_extractjson
sql
SELECT JSON_EXTRACT(settings, '$.notifications.email') AS email_enabled FROM user_preferences;

Returns JSON data; use `->>`-style unquoting equivalents or JSON_UNQUOTE when needed.

Update a JSON path

Write or replace a value inside a JSON document.

sqlANYjson_setupdate-json
sql
UPDATE user_preferences
SET settings = JSON_SET(settings, '$.theme', 'dark')
WHERE user_id = 42;

Useful for gradually evolving settings blobs without replacing the whole document manually.

Build JSON in a query

Construct a JSON object from scalar values.

sqlANYjson_objectapi
sql
SELECT JSON_OBJECT('id', id, 'email', email) AS user_json FROM users LIMIT 5;

Helpful for API prototyping, exports, and SQL-side reshaping.

Concatenate strings

Join scalar values into one string.

sqlANYconcatstrings
sql
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM contacts;

A basic but common formatting function.

Use COALESCE for fallback values

Return the first non-NULL expression.

sqlANYcoalescenulls
sql
SELECT COALESCE(display_name, full_name, email) AS label FROM users;

Great for optional columns and layered display fallbacks.

Map values with CASE

Transform values into labels inside a query.

sqlANYcasemapping
sql
SELECT id,
  CASE status
    WHEN 'paid' THEN 'Paid'
    WHEN 'pending' THEN 'Pending'
    ELSE 'Other'
  END AS status_label
FROM orders;

`CASE` is essential for SQL-side normalization and reporting.

Recommended next

No recommendations yet.