Skip to content

Instantly share code, notes, and snippets.

@sfaut
Created March 4, 2023 23:58
Show Gist options
  • Save sfaut/5f661074f03d1200942265e9b0ab9779 to your computer and use it in GitHub Desktop.
Save sfaut/5f661074f03d1200942265e9b0ab9779 to your computer and use it in GitHub Desktop.
Converts a number of seconds since year 0 to a DATETIME
-- Converts a number of seconds since year 0 to a DATETIME
-- MySQL counterpart of TO_SECONDS() : https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_to-seconds
-- Author : sfaut <https://github.com/sfaut>
-- Publication date : 2023-03-05
-- Tested with MySQL 8.0.32 (@@sql_mode='ANSI,TRADITIONAL')
CREATE FUNCTION FROM_SECONDS(seconds BIGINT)
RETURNS DATETIME
DETERMINISTIC
COMMENT 'Converts a number of seconds since year 0 to a DATETIME'
RETURN DATE'0000-01-01' + INTERVAL seconds SECOND - INTERVAL 1 DAY;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment