MySQL query to return the entire date and time based on a string and format



Let us first create a table −

mysql> create table DemoTable    -> (    -> AdmissionDate varchar(100)    -> ); Query OK, 0 rows affected (0.66 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Wed, 19 Jun 2019 04:10:20'); Query OK, 1 row affected (0.22 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+---------------------------+ | AdmissionDate | +---------------------------+ | Wed, 19 Jun 2019 04:10:20 | +---------------------------+ 1 row in set (0.00 sec)

Following is the to return date and time based on string and format −

mysql> SELECT STR_TO_DATE(AdmissionDate, '%a, %d %b %Y %H:%i:%s EDT') from DemoTable;

Output

This will produce the following output −

+---------------------------------------------------------+ | STR_TO_DATE(AdmissionDate, '%a, %d %b %Y %H:%i:%s EDT') | +---------------------------------------------------------+ | 2019-06-19 04:10:20 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
Updated on: 2020-06-30T12:36:03+05:30

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements