Lesson

MySQL LOCALTIMESTAMP() Function

10 min reading
Free Course

MySQL LOCALTIMESTAMP(): Capture the Session Timestamp

LOCALTIMESTAMP and LOCALTIMESTAMP() return the current date and time in the MySQL session's time zone. Like LOCALTIME(), it is a direct synonym for NOW() and CURRENT_TIMESTAMP(), supporting optional fractional seconds up to six digits.

Audit logs and employee activity trackers use LOCALTIMESTAMP() for explicit SQL-standard timestamping in queries and table defaults.

Syntax

Call LOCALTIMESTAMP with or without parentheses, or pass a precision literal 0 through 6:

LOCALTIMESTAMP
LOCALTIMESTAMP()
LOCALTIMESTAMP(fsp)
  • fsp: Optional integer literal from 0 to 6 for fractional seconds.

In string context, it produces 'YYYY-MM-DD HH:MM:SS[.ffffff]', and in numeric context (LOCALTIMESTAMP + 0), it produces YYYYMMDDHHMMSS[.ffffff].

Practical Example

Suppose your HR portal records badge audits for active staff in demo_employees, stamping each row with microsecond-level precision (fsp = 3).

SELECT
    employee_id,
    full_name,
    last_seen_at,
    LOCALTIMESTAMP(3) AS audit_recorded_at
FROM demo_employees
WHERE employee_id IN (11, 12);
employee_id full_name last_seen_at audit_recorded_at
11 Nadia Rahman 2026-05-12 08:55:10 2026-05-12 10:22:19.804
12 Tariq Hasan NULL 2026-05-12 10:22:19.804

Even though employee 12 has a NULL value in last_seen_at, LOCALTIMESTAMP(3) still outputs the full audit timestamp cleanly.

Common Mistakes & NULL Behavior

Keep these rules in mind when using LOCALTIMESTAMP():

  • Takes no required inputs (immune to NULL columns): NULL values in any table column cannot make LOCALTIMESTAMP() fail or return NULL.
  • Invalid fsp raises a syntax error: Only integer literals 0 to 6 are accepted. Passing LOCALTIMESTAMP(NULL) or LOCALTIMESTAMP(9) is invalid.
  • Constant per statement, not per row: Even in long-running INSERT or UPDATE statements, LOCALTIMESTAMP() returns the moment the statement started, not the wall clock for each row.
  • Do not confuse with UTC: LOCALTIMESTAMP() uses @@session.time_zone. Call UTC_TIMESTAMP() when storing zone-independent UTC timestamps as DATETIME.

Frequently Asked Questions

Can I use LOCALTIMESTAMP as a column DEFAULT value?

Yes. In MySQL, LOCALTIMESTAMP and LOCALTIMESTAMP(fsp) are valid synonyms for CURRENT_TIMESTAMP in DEFAULT and ON UPDATE column definitions.

What is the difference between LOCALTIMESTAMP() and SYSDATE()?

LOCALTIMESTAMP() returns the statement start time and stays constant throughout the query, whereas SYSDATE() returns the live execution time at each evaluation.

Does LOCALTIMESTAMP() include the time zone offset like '+00:00'?

No. MySQL DATETIME values never embed a time zone offset suffix; they format strictly as 'YYYY-MM-DD HH:MM:SS[.ffffff]'.

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