MySQL query to convert timestamp to month?


To convert timestamp to month, use the FROM_UNIXTIME() method as in the below syntax −

select month(from_unixtime(yourColumnName)) from yourTableName;

Let us first create a table −

mysql> create table DemoTable1457
   -> (
   -> Value bigint
   -> );
Query OK, 0 rows affected (0.85 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1457 values(1570207117);
Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable1457 values(1548947534);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1457 values(1575213134);
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1457;

This will produce the following output −

+------------+
| Value      |
+------------+
| 1570207117 |
| 1548947534 |
| 1575213134 |
+------------+
3 rows in set (0.00 sec)

Following is the query to convert timestamp to month −

mysql> select month(from_unixtime(Value)) from DemoTable1457;

This will produce the following output −

+-----------------------------+
| month(from_unixtime(Value)) |
+-----------------------------+
|                          10 |
|                           1 |
|                          12 |
+-----------------------------+
3 rows in set (0.00 sec)

Updated on: 10-Dec-2019

623 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements