Lesson

MySQL TIMESTAMP() Function

10 min reading
Free Course

MySQL TIMESTAMP() Function

The MySQL TIMESTAMP() function converts a DATE or DATETIME expression into a DATETIME value. With a second TIME argument, it adds that time offset to the first expression before returning the combined DATETIME.

Developers use TIMESTAMP(date_col, time_col) when tables store dates and clock times in separate columns and need a single DATETIME value for sorting or range checks.

Even though the function is named TIMESTAMP(), it returns a DATETIME value (not a Unix integer and not subject to the 2038 TIMESTAMP column limit).

Syntax

TIMESTAMP(expr)
TIMESTAMP(expr1, expr2)
  • expr / expr1: A valid DATE or DATETIME expression.
  • expr2: Optional TIME expression to add to expr1 (can be positive or negative).

Practical Example

Suppose demo_orders stores delivery_date (DATE) and delivery_slot (TIME) separately. You can combine them into one DATETIME column and also compute a midnight cutoff using the one-argument form.

SELECT
    order_id,
    TIMESTAMP(delivery_date) AS day_start,
    TIMESTAMP(delivery_date, delivery_slot) AS scheduled_at
FROM demo_orders
WHERE order_id IN (510, 511);
order_id day_start scheduled_at
510 2026-05-12 00:00:00 2026-05-12 14:30:00
511 2026-05-13 00:00:00 2026-05-13 09:00:00

Notice how TIMESTAMP(delivery_date) appends '00:00:00' automatically to turn a DATE into a DATETIME.

Common Mistakes & NULL Behavior

Do not confuse the TIMESTAMP() function with UNIX_TIMESTAMP(). TIMESTAMP('2026-05-10') returns '2026-05-10 00:00:00', not epoch seconds.

When using the two-argument form, expr2 must be a valid TIME value within -838:59:59 to 838:59:59. Passing a DATETIME as expr2 (instead of a TIME) fails validation and returns NULL.

TIMESTAMP() returns NULL if any provided argument is NULL, if expr1 is an invalid date such as '2026-02-30', or if expr2 cannot parse as a TIME. Remember that passing a negative expr2 like '-02:00:00' subtracts two hours from expr1, rolling back into the previous calendar day if needed.

Frequently Asked Questions

Can expr1 already be a DATETIME when calling TIMESTAMP(expr1, expr2)?

Yes. TIMESTAMP('2026-05-10 10:00:00', '02:30:00') adds the time offset and returns '2026-05-10 12:30:00'.

Does TIMESTAMP() accept negative time values in expr2?

Yes. Calling TIMESTAMP('2026-05-10 01:00:00', '-03:00:00') returns '2026-05-09 22:00:00'.

What is the difference between TIMESTAMP(d, t) and ADDTIME(d, t)?

Both can add a TIME to a DATETIME, but TIMESTAMP() also accepts a pure DATE for expr1 without failing validation.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum