Tutorialspoint

1 Answer
Kumar Varma

You can use format specifier. Following is the syntax −

select str_to_date(yourColumnName,'%d/%m/%Y %h:%i %p') as anyAliasName from yourTableName;

Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values('11/02/2019 10:35');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('21/12/2018 12:01');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------------+
| DueDate          |
+------------------+
| 11/02/2019 10:35 |
| 21/12/2018 12:01 |
+------------------+
2 rows in set (0.00 sec)

Following is the query to convert String to Time in MySQL.

mysql> select str_to_date(DueDate,'%d/%m/%Y %h:%i %p') as ConvertDate from DemoTable;

This will produce the following output −

+---------------------+
| ConvertDate         |
+---------------------+
| 2019-02-11 10:35:00 |
| 2018-12-21 00:01:00 |
+---------------------+
2 rows in set (0.00 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.