Extract Numeric Date Value from Date Format in MySQL?



For this, use UNIX_TIMESTAMP(). Following is the syntax −

select UNIX_TIMESTAMP(STR_TO_DATE(yourColumnName, "%d-%b-%y")) as anyAliasName from yourTableName;

Let us create a table −

mysql> create table demo34
−> (
−> datevalue varchar(40)
−> );
Query OK, 0 rows affected (1.51 sec)

Insert some records into the table with the help of insert command −

mysql> insert into demo34 values('31−Jan−19');
Query OK, 1 row affected (0.13 sec)

mysql> insert into demo34 values('03−Mar−21');
Query OK, 1 row affected (0.12 sec)

mysql> insert into demo34 values('13−Jun−20');
Query OK, 1 row affected (0.11 sec)

Display records from the table using select statement −

mysql> select *from demo34;

This will produce the following output −

+-----------+
| datevalue |
+-----------+
| 31−Jan−19 |
| 03−Mar−21 |
| 13−Jun−20 |
+-----------+
3 rows in set (0.00 sec)

Following is the query to extract numeric date value from date format −

mysql> select UNIX_TIMESTAMP(STR_TO_DATE( datevalue, "%d-%b-%y")) as Numeric_Date from demo34;

This will produce the following output −

+--------------+
| Numeric_Date |
+--------------+
| 1548873000   |
| 1614709800   |
| 1591986600   |
+--------------+
3 rows in set (0.00 sec)

Advertisements