
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to convert string to 24-hour datetime format in MySQL?
To convert string to 24 hour datetime format in MySQL, you can use STR_TO_DATE() method. With that use the following format for datetime as the parameter:
'%Y-%m-%d %H:%i:%s'
Following is the syntax
SELECT STR_TO_DATE(yourColumnName, '%Y-%m-%d %H:%i:%s') FROM yourTableName;
Let us first create a table:
mysql> create table DemoTable (ArrivalDate varchar(200)); Query OK, 0 rows affected (0.57 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable values('2019-01-31 15:45:23'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('2012-12-12 20:30:26'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('2016-06-07 21:04:05'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2017-02-03 19:24:32'); Query OK, 1 row affected (0.27 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output
+---------------------+ | ArrivalDate | +---------------------+ | 2019-01-31 15:45:23 | | 2012-12-12 20:30:26 | | 2016-06-07 21:04:05 | | 2017-02-03 19:24:32 | +---------------------+ 4 rows in set (0.00 sec)
Following is the query to convert string to 24-hour datetime format in MySQL:
mysql> select str_to_date(ArrivalDate,'%Y-%m-%d %H:%i:%s') AS `24HourFormat` from DemoTable;
This will produce the following output:
+---------------------+ | 24HourFormat | +---------------------+ | 2019-01-31 15:45:23 | | 2012-12-12 20:30:26 | | 2016-06-07 21:04:05 | | 2017-02-03 19:24:32 | +---------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Python program to convert time from 12 hour to 24 hour format
- C++ program to convert time from 12 hour to 24 hour format
- C# program to convert time from 12 hour to 24 hour format
- How to Convert Time Format from 12 Hour to 24 Hour and Vice Versa in Excel?
- Convert time from 24 hour clock to 12 hour clock format in C++
- How to Convert Text Datetime Format to Real Datetime Format in Excel?
- Converting 12 hour format time to 24 hour format in JavaScript
- How to convert MySQL DATETIME value to JSON format in JavaScript?
- How to convert C# DateTime to “YYYYMMDDHHMMSS” format?
- How to convert JavaScript datetime to MySQL datetime?
- How to convert 12-hour time scale to 24-hour time in R?
- Java Program to display time in 24-hour format
- How to Convert yyyymmddhhmmss Date Format to Normal Datetime in Excel?Datetime in Excel?
- MySQL convert timediff output to day, hour, minute, second format?
- Format hour in k (1-24) format in Java

Advertisements