
- 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
Format MySQL date and convert to year-month-day
Let us first create a table −
mysql> create table DemoTable666(AdmissionDate varchar(200)); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable666 values('Sat,20 Jul 2019 04:29:35'); Query OK, 1 row affected (1.12 sec) mysql> insert into DemoTable666 values('Fri,02 Oct 2018 12:19:15'); Query OK, 1 row affected (1.05 sec) mysql> insert into DemoTable666 values('Sun,01 Aug 2016 11:10:05'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable666 values('Fri, 06 Nov 2015 04:06:05 -0500'); Query OK, 1 row affected (0.24 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable666;
This will produce the following output −
+---------------------------------+ | AdmissionDate | +---------------------------------+ | Sat,20 Jul 2019 04:29:35 | | Fri,02 Oct 2018 12:19:15 | | Sun,01 Aug 2016 11:10:05 | | Fri, 06 Nov 2015 04:06:05 -0500 | +---------------------------------+ 4 rows in set (0.00 sec)
Following is the query to convert the above date format to year-month-day −
mysql> select DATE_FORMAT(STR_TO_DATE(AdmissionDate, '%a, %d %b %Y %H:%i:%S'),'%Y-%m-%d %H:%i:%s') AS MyDate from DemoTable666;
This will produce the following output −
+---------------------+ | MyDate | +---------------------+ | 2019-07-20 04:29:35 | | 2018-10-02 12:19:15 | | 2016-08-01 11:10:05 | | 2015-11-06 04:06:05 | +---------------------+ 4 rows in set, 1 warning (0.00 sec)
- Related Articles
- Create date from day, month, year fields in MySQL?
- How to Convert Numbers to Year/Month/Day or Date in Excel?
- How to convert year, month, and day of the month into a complete date in R?
- Convert day of year to day of month in Java
- Finding day of week from date (day, month, year) in JavaScript
- MySQL query to fetch date with year and month?
- How to get a Date from year, month and day in Java?
- Combine three strings (day, month, year) and calculate next date PHP?
- Format date in MySQL to return MonthName and Year?
- MySQL query to convert YYYY-MM-DD to DD Month, YYYY date format
- MySQL extract year from date format?
- Convert MySQL Unix-Timestamp format to date format?
- Filter the records of current day, month and year in MySQL?
- Compare only day and month with date field in MySQL?
- How to get day of month, day of year and day of week in android using offset date time API class?

Advertisements