- MySQL Basics
- MySQL - Home
- MySQL - Introduction
- MySQL - Features
- MySQL - Versions
- MySQL - Variables
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Node.js Syntax
- MySQL - Java Syntax
- MySQL - Python Syntax
- MySQL - Connection
- MySQL - Workbench
- MySQL Databases
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Show Database
- MySQL - Copy Database
- MySQL - Database Export
- MySQL - Database Import
- MySQL - Database Info
- MySQL Users
- MySQL - Create Users
- MySQL - Drop Users
- MySQL - Show Users
- MySQL - Change Password
- MySQL - Grant Privileges
- MySQL - Show Privileges
- MySQL - Revoke Privileges
- MySQL - Lock User Account
- MySQL - Unlock User Account
- MySQL Tables
- MySQL - Create Tables
- MySQL - Show Tables
- MySQL - Alter Tables
- MySQL - Rename Tables
- MySQL - Clone Tables
- MySQL - Truncate Tables
- MySQL - Temporary Tables
- MySQL - Repair Tables
- MySQL - Describe Tables
- MySQL - Add/Delete Columns
- MySQL - Show Columns
- MySQL - Rename Columns
- MySQL - Table Locking
- MySQL - Drop Tables
- MySQL - Derived Tables
- MySQL Queries
- MySQL - Queries
- MySQL - Constraints
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Replace Query
- MySQL - Insert Ignore
- MySQL - Insert on Duplicate Key Update
- MySQL - Insert Into Select
- MySQL Indexes
- MySQL - Indexes
- MySQL - Create Index
- MySQL - Drop Index
- MySQL - Show Indexes
- MySQL - Unique Index
- MySQL - Clustered Index
- MySQL - Non-Clustered Index
- MySQL Operators and Clauses
- MySQL - Where Clause
- MySQL - Limit Clause
- MySQL - Distinct Clause
- MySQL - Order By Clause
- MySQL - Group By Clause
- MySQL - Having Clause
- MySQL - AND Operator
- MySQL - OR Operator
- MySQL - Like Operator
- MySQL - IN Operator
- MySQL - ANY Operator
- MySQL - EXISTS Operator
- MySQL - NOT Operator
- MySQL - NOT EQUAL Operator
- MySQL - IS NULL Operator
- MySQL - IS NOT NULL Operator
- MySQL - Between Operator
- MySQL - UNION Operator
- MySQL - UNION vs UNION ALL
- MySQL - MINUS Operator
- MySQL - INTERSECT Operator
- MySQL - INTERVAL Operator
- MySQL Joins
- MySQL - Using Joins
- MySQL - Inner Join
- MySQL - Left Join
- MySQL - Right Join
- MySQL - Cross Join
- MySQL - Full Join
- MySQL - Self Join
- MySQL - Delete Join
- MySQL - Update Join
- MySQL - Union vs Join
- MySQL Keys
- MySQL - Unique Key
- MySQL - Primary Key
- MySQL - Foreign Key
- MySQL - Composite Key
- MySQL - Alternate Key
- MySQL Triggers
- MySQL - Triggers
- MySQL - Create Trigger
- MySQL - Show Trigger
- MySQL - Drop Trigger
- MySQL - Before Insert Trigger
- MySQL - After Insert Trigger
- MySQL - Before Update Trigger
- MySQL - After Update Trigger
- MySQL - Before Delete Trigger
- MySQL - After Delete Trigger
- MySQL Data Types
- MySQL - Data Types
- MySQL - VARCHAR
- MySQL - BOOLEAN
- MySQL - ENUM
- MySQL - DECIMAL
- MySQL - INT
- MySQL - FLOAT
- MySQL - BIT
- MySQL - TINYINT
- MySQL - BLOB
- MySQL - SET
- MySQL Regular Expressions
- MySQL - Regular Expressions
- MySQL - RLIKE Operator
- MySQL - NOT LIKE Operator
- MySQL - NOT REGEXP Operator
- MySQL - regexp_instr() Function
- MySQL - regexp_like() Function
- MySQL - regexp_replace() Function
- MySQL - regexp_substr() Function
- MySQL Fulltext Search
- MySQL - Fulltext Search
- MySQL - Natural Language Fulltext Search
- MySQL - Boolean Fulltext Search
- MySQL - Query Expansion Fulltext Search
- MySQL - ngram Fulltext Parser
- MySQL Functions & Operators
- MySQL - Date and Time Functions
- MySQL - Arithmetic Operators
- MySQL - Numeric Functions
- MySQL - String Functions
- MySQL - Aggregate Functions
- MySQL Misc Concepts
- MySQL - NULL Values
- MySQL - Transactions
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - SubQuery
- MySQL - Comments
- MySQL - Check Constraints
- MySQL - Storage Engines
- MySQL - Export Table into CSV File
- MySQL - Import CSV File into Database
- MySQL - UUID
- MySQL - Common Table Expressions
- MySQL - On Delete Cascade
- MySQL - Upsert
- MySQL - Horizontal Partitioning
- MySQL - Vertical Partitioning
- MySQL - Cursor
- MySQL - Stored Functions
- MySQL - Signal
- MySQL - Resignal
- MySQL - Character Set
- MySQL - Collation
- MySQL - Wildcards
- MySQL - Alias
- MySQL - ROLLUP
- MySQL - Today Date
- MySQL - Literals
- MySQL - Stored Procedure
- MySQL - Explain
- MySQL - JSON
- MySQL - Standard Deviation
- MySQL - Find Duplicate Records
- MySQL - Delete Duplicate Records
- MySQL - Select Random Records
- MySQL - Show Processlist
- MySQL - Change Column Type
- MySQL - Reset Auto-Increment
- MySQL - Coalesce() Function
MySQL - FROM_UNIXTIME() Function
The MySQL FROM_UNIXTIME() function is used to return a representation of UNIX timestamp as a datetime or character string value. UNIX timestamp is nothing but an internal timestamp value representing number of seconds since '1970-01-01 00:00:00' UTC.
This function accepts a UNIX timestamp as argument and the format as an optional argument, converts it into datetime value or a character string (according to the format, if given) and returns the result.
Special cases of this function includes −
If format is omitted, this function returns a DATETIME value.
If UNIX timestamp or format is NULL, this function returns NULL.
If UNIX timestamp is an integer, the fractional seconds precision of the DATETIME is zero.
When UNIX timestamp is a decimal value, the fractional seconds precision of the DATETIME is the same as the precision of the decimal value, up to a maximum of 6.
When UNIX timestamp is a floating point number, the fractional seconds precision of the datetime is 6.
Syntax
Following is the syntax of MySQL FROM_UNIXTIME() function −
FROM_UNIXTIME(unix_timestamp, [format])
Parameters
This method accepts two parameters. The same is described below −
unix_timestamp: The UNIX timestamp, representing the number of seconds since January 1, 1970, 00:00:00 UTC.
format (optional): The desired format for the output. It's a string specifying how the result should be formatted. If omitted, the default format is 'YYYY-MM-DD HH:MM:SS'.
Return value
This function returns a DATETIME value representing the date and time corresponding to the given UNIX timestamp.
Example
Following example demonstrates the usage of the FROM_UNIXTIME() function −
SELECT FROM_UNIXTIME(0) As Result;
Output
Following output is obtained −
Result |
---|
1970-01-01 05:30:00 |
Example
Following is another example of this function, where we are converting the Unix timestamp 1351708200 into a date and time format −
SELECT FROM_UNIXTIME(1351708200) As Result;
Output
Following output is obtained −
Result |
---|
2012-11-01 00:00:00 |
Example
We can also pass the date-time expression as an argument to this function −
SELECT FROM_UNIXTIME(1441426245.2300) As Result;
Output
This will produce the following result −
Result |
---|
2015-09-05 09:40:45.2300 |
Example
We can also pass timestamp values in the form of a string as shown below −
SELECT FROM_UNIXTIME('463455563') As Result;
Output
Following output is obtained −
Result |
---|
1984-09-08 07:09:23.000000 |
Example
We can also pass the column name as an argument to this function. Let us create a table named MyPlayers using CREATE statement as shown below −
CREATE TABLE MyPlayers( ID INT, NAME VARCHAR(255), DOBTimestamp VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
Now, insert the following records into the MyPlayers table using the INSERT statement −
INSERT INTO MyPlayers VALUES (1, 'Shikhar Dhawan', '376338600', 'Delhi', 'India'), (2, 'Jonathan Trott', '356725800', 'CapeTown', 'SouthAfrica'), (3, 'Kumara Sangakkara', '246738600', 'Matale', 'Srilanka'), (4, 'Virat Kohli', '594671400', 'Delhi', 'India'), (5, 'Rohit Sharma', '546719400', 'Nagpur', 'India'), (6, 'Ravindra Jadeja', '597349800', 'Nagpur', 'India'), (7, 'James Anderson', '394223400', 'Burnley', 'England');
Execute the below query to fetch all the inserted records in the above-created table −
Select * From MyPlayers;
Following is the ORDERS table −
ID | NAME | DOBTimestamp | Place_Of_Birth | Country |
---|---|---|---|---|
1 | Shikhar Dhawan | 376338600 | Delhi | India |
2 | Jonathan Trott | 356725800 | CapeTown | SouthAfrica |
3 | Kumara Sangakkara | 246738600 | Matale | Srilanka |
4 | Virat Kohli | 594671400 | Delhi | India |
5 | Rohit Sharma | 546719400 | Nagpur | India |
6 | Ravindra Jadeja | 597349800 | Nagpur | India |
7 | James Anderson | 394223400 | Burnley | England |
Now, we use the MySQL FROM_UNIXTIME() function to convert the values of a DOBTimestamp column into datetime values −
SELECT ID, NAME, DOBTimestamp, FROM_UNIXTIME(DOBTimestamp) As Result From MyPlayers;
Output
The output is displayed as follows −
ID | NAME | DOBTimestamp | Result |
---|---|---|---|
1 | Shikhar Dhawan | 376338600 | 1981-12-05 00:00:00.000000 |
2 | Jonathan Trott | 356725800 | 1981-04-22 00:00:00.000000 |
3 | Kumara Sangakkara | 246738600 | 1977-10-27 00:00:00.000000 |
4 | Virat Kohli | 594671400 | 1988-11-05 00:00:00.000000 |
5 | Rohit Sharma | 546719400 | 1987-04-30 00:00:00.000000 |
6 | Ravindra Jadeja | 597349800 | 1988-12-06 00:00:00.000000 |
7 | James Anderson | 394223400 | 1982-06-30 00:00:00.000000 |