I have a table with a date column that is formatted as a DATETIME(3)
. I would like to convert it to a BIGINT(13)
that stores that datetime as the milliseconds from unix epoch.
How can I modify the column definition and convert all of the existing values in the table without losing any of the data?
I know you can convert the datetime to milliseconds using taking into account the timezone:
SELECT CAST(
UNIX_TIMESTAMP(
CONVERT_TZ('2019-04-22 00:37:47.843', '+00:00', 'SYSTEM')
) * 1000 AS INT
);
Which returns: 1555893467843
The table is created as such:
CREATE TABLE `usages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uuid` varchar(36) NOT NULL,
`datetime` datetime(3) NOT NULL,
PRIMARY KEY (`id`),
);
I would like it to look like this:
CREATE TABLE `usages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uuid` varchar(36) NOT NULL,
`unix` bigint(13) NOT NULL,
PRIMARY KEY (`id`),
);