How to Convert Epoch Time to Date in SQL?
Last Updated :
03 Jun, 2024
To convert Epoch Time to Date in SQL use the DATEADD() function. DATEADD() function in SQL Server is used to add a time or date interval to a specified date and return the modified date.
Some features of the SQL DATEADD() function are:
- This function is used to sum up a time or a date interval to a date specified.
- This function comes under Date Functions.
- This function accepts three parameters namely interval, number, and date.
- This function can also include time in the interval section.
Syntax
The syntax to convert Epoch Time to Date in SQL is:
DATEADD(interval, number, date)
Convert Epoch Time to Date in SQL Example
Here we will see, how to convert epoch time to date in SQL Server using the DATEADD() function.
First, we will create an EpochDB table in a database called “geeks“.
CREATE DATABASE geeks;
Use the below SQL statement to switch the database context to geeks:
USE geeks;
Creating Table
We have the following EpochDB in our geeks database.
CREATE TABLE EpochDOB (
Id INT,
Person VARCHAR(50),
Dt BIGINT
);

Add data to the table
Use the below statement to add data to the EpochDB table:
INSERT INTO EpochDOB VALUES
(1,'Anuj',848698632000),
(2,'Harsh',957532509000),
(3,'Ravi',1547455833000);
To verify the contents of the table use the below statement
SELECT * FROM EpochDOB;

SQL Query to Convert Epoch Time to Date in SQL
Because our Epoch time is specified in milliseconds, we may convert it to seconds. To convert milliseconds to seconds, first, divide the millisecond count by 1000.
Later, we use DATEADD() to add the number of seconds since the epoch, which is January 1, 1970 and cast the result to retrieve the date since the epoch.
SELECT *, CAST(DATEADD(SECOND, Dt/1000
,'1970/1/1') AS DATE) DOBDate
FROM EpochDOB;
Similar Reads
How to Convert Timestamp to Datetime in MySQL? In this article, we are going to learn how to convert Timestamp to Datetime in MySQL.To execute these queries, we need to first add integer data(written in timestamp format) and then use the FROM_UNIXTIME() function to convert it into "Datetime" Data Type. FROM_UNIXTIME(): This function in MySQL ret
2 min read
SQL Query to Convert Datetime to Epoch Converting a datetime value to Epoch time is a common operation in SQL, particularly when working with timestamps in various applications. In this article, We will learn a step-by-step process of creating a SQL database, inserting datetime values and converting those values into Epoch time using SQL
3 min read
How to Get Current Date and Time in SQL? Managing date and time efficiently is crucial for any database system. SQL provides built-in functions to retrieve the current date and time, which are especially useful for applications involving logging, reporting, and auditing. In this article, we will explain the three main SQL functions to fetc
4 min read
How To Get the Current Date Without Time in T-SQL? To get the current date in T-SQL use the GETDATE() function. The result of the function is DateTime data type meaning, it contains both the date and the time, e.g. 2022-07-10 10:32:04. However, to get the current date without time in T-SQL use the CAST() function. This function takes any expression
1 min read
How to Strip Time Component From Datetime in MySQL? MySQL is a very popular open-source and free database server that is ideal for small and large applications and is a relational database management system where SQL (Structured Query Language) queries or statements are used to manage and manipulate the data. MySQL provides many built-in functions li
4 min read