
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to insert date record to the same table with different date formats with MySQL?
For this, you can use the INSERT INTO SELECT statement. To format the date, use the DATE_FORMAT() function. Let us first create a table −
mysql> create table DemoTable -> ( -> DateOfJoining datetime, -> JoiningDate text -> ); Query OK, 0 rows affected (0.79 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(DateOfJoining) values('2019-10-26 13:52:10'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(DateOfJoining) values('2018-12-31 15:20:40'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement−
mysql> select *from DemoTable;
This will produce the following output −
+---------------------+-------------+ | DateOfJoining | JoiningDate | +---------------------+-------------+ | 2019-10-26 13:52:10 | NULL | | 2018-12-31 15:20:40 | NULL | +---------------------+-------------+ 2 rows in set (0.00 sec)
Here is the query to insert date record to the same table with different date formats −
mysql> insert into DemoTable(JoiningDate) select date_format(DateOfJoining , '%W %e, %b %Y @ %r') from DemoTable; Query OK, 2 rows affected (0.13 sec) Records: 2 Duplicates: 0 Warnings: 0
Let us check the table records once again−
mysql> select *from DemoTable;
This will produce the following output−
+---------------------+-------------------------------------+ | DateOfJoining | JoiningDate | +---------------------+-------------------------------------+ | 2019-10-26 13:52:10 | NULL | | 2018-12-31 15:20:40 | NULL | | NULL | Saturday 26, Oct 2019 @ 01:52:10 PM | | NULL | Monday 31, Dec 2018 @ 03:20:40 PM | +---------------------+-------------------------------------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- How to insert date in single quotes with MySQL date formats?
- How to insert DATE in MySQL table with TRIGGERS?
- How to change the date in a table with date records with MySQL?
- MySQL query to insert row with date?
- How to search a record by date in MySQL table?
- Insert record in a MySQL table with Java
- Java Program to set date formats for different countries
- Display record with today and tomorrow’s date from a column with date record in MySQL
- MySQL select * and find record with current date
- JavaScript Date Formats
- MySQL query to fetch the latest date from a table with date records
- Add 30 days to date in a MySQL table with arrival date records
- How to insert a document with date in MongoDB?
- How to select a date less than the current date with MySQL?
- How to create a table with date column?
Advertisements