How to Convert Timestamp to Datetime in MySQL?
Last Updated :
25 Jan, 2022
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 returns a Datetime representation of a Unix Timestamp that has been entered as a parameter.
Step 1: We will create a Database
For the creation of a database, we will use the following query
Query:
CREATE DATABASE custom_db;

Step 2: Use the created Database
Now we use a created database with the help of the following query.
Query:
USE custom_db;

Step 3: Creating a table inside the database
We will create a table with a single column having datatype int for storing the timestamp values. We use the following query to create a table:
Query:
CREATE TABLE custom_table
(TIMESTAMP_VAL INT);

Step 4: Insertion of data into the created table
We will insert data in timestamp format into the table by using the following query:
Query:
INSERT INTO custom_table
VALUES(1242187029),
(1692076451),
(1434021855);

Step 5:
Here we need to show data in another format, so we first select it using the "SELECT statement" in MySQL. We will then pass the selected data from the TIMESTAMP_VAL column as a parameter in FROM_UNIXTIME() function. The syntax of the FROM_UNIXTIME() is :
Syntax:
FROM_UNIXTIME(timestamp,format)
The format is NOT compulsory here. If we do not write the format, the query used will be :
Query I:
SELECT FROM_UNIXTIME(TIMESTAMP_VAL)
FROM custom_table;

If we use the format '%Y %D %M %h:%i:%s' , then the query used will be:
Query II:
SELECT FROM_UNIXTIME(TIMESTAMP_VAL,
'%Y %D %M %h:%i:%s') FROM custom_table;
Similar Reads
How to Convert DateTime to UNIX Timestamp in Python ? Generating a UNIX timestamp from a DateTime object in Python involves converting a date and time representation into the number of seconds elapsed since January 1, 1970 (known as the Unix epoch). For example, given a DateTime representing May 29, 2025, 15:30, the UNIX timestamp is the floating-point
2 min read
How to Convert Epoch Time to Date in SQL? 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 d
2 min read
Convert datetime to unix timestamp in SQLAlchemy model When dealing with databases date and time are considered to be one of the most important attributes for any entity. With such data, we often encounter some common task of converting DateTime to a Unix timestamp. In this article, we will learn how we can convert datetime to Unix timestamp in SQLAlche
5 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
How to Update Current Timestamp in MySQL? MySQL is an easy-to-use RDBMS. Many organizations prefer to use it because of its easy maintainability, easier to prepare schemas, stored procedures, triggers, and database maintenance. In this article, let us see how to update to Current Timestamp in MySQL. Step 1: Database creation Firstly we crea
3 min read