DATEADD() function in SQL Server is used to sum up a time or a date interval to a specified date then returns the modified date. There are some features of DATEADD() below:
- 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.
Here we will see, how to convert epoch time to date in SQL Server using the DATEADD() function. For the purpose of demonstration, we will be creating an EpochDB table in a database called “geeks“.
Step 1: Creating the Database
Use the below SQL statement to create a database called geeks:
CREATE DATABASE geeks;
Step 2: Using the Database
Use the below SQL statement to switch the database context to geeks:
Step 3: Table Definition
We have the following EpochDB in our geeks database.
CREATE TABLE EpochDOB (
Step 4: Adding data to the table
Use the below statement to add data to the EpochDB table:
INSERT INTO EpochDOB VALUES
Step 5: To verify the contents of the table use the below statement
SELECT * FROM EpochDOB;
Step 6: Result
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
Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation
Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation