mysqldude

Convert TIMESTAMP to INTEGER

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
CAST(timestamp AS UNSIGNED) gives YYYYMMDDhhmmss (original)
original post
✓✓✓✓✓✓✓✓✓
CAST(CURRENT_TIMESTAMP AS UNSIGNED) is a 14-digit number (original)
original post
✓✓✓✓✓✓✓✓✓
UNIX_TIMESTAMP() for seconds since 1970 (usually what is meant)
modern rewrite
✓✓✓✓✓✓✓✓✓
CAST of a timestamp with .654321 seconds: MySQL rounds up (…28), MariaDB truncates (…27)
version check
✓✓✓✓✓≠≠≠≠
CAST(timestamp AS UNSIGNED) gives YYYYMMDDhhmmss (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
CAST(CURRENT_TIMESTAMP AS UNSIGNED) is a 14-digit number (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
UNIX_TIMESTAMP() for seconds since 1970 (usually what is meant) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
CAST of a timestamp with .654321 seconds: MySQL rounds up (…28), MariaDB truncates (…27) version check
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6≠10.11≠11.4≠11.8≠
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (1)
  • CAST of a timestamp with .654321 seconds: MySQL rounds up (…28), MariaDB truncates (…27) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): rows differ

The original article

First published 2012-04-20. The text below is the original; the table above shows how it behaves on current versions.

I have seen this question raised several times. How can we convert a timestamp to an INTEGER. This is achieved by using the CAST function available in MySQL.

Here is an example:

mysql> select cast(current_timestamp as unsigned);
+-------------------------------------+
| cast(current_timestamp as unsigned) |
+-------------------------------------+
|                      20120420174927 |
+-------------------------------------+
1 row in set (0.00 sec)