

- 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
The easiest way to insert date records in MySQL?
Use the STR_TO_DATE() method to insert date records as in the below syntax −
select str_to_date(yourColumnName,'%b %Y') from yourTableName;
Let us first create a table −
mysql> create table DemoTable -> ( -> JoiningYear varchar(20) -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Jan 2018'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('May 2107'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Aug 2019'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Oct 2020'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+ | JoiningYear | +-------------+ | Jan 2018 | | May 2107 | | Aug 2019 | | Oct 2020 | +-------------+ 4 rows in set (0.00 sec)
Here is the query to insert date records in MySQL −
mysql> select str_to_date(JoiningYear,'%b %Y') from DemoTable;
This will produce the following output −
+----------------------------------+ | str_to_date(JoiningYear,'%b %Y') | +----------------------------------+ | 2018-01-00 | | 2107-05-00 | | 2019-08-00 | | 2020-10-00 | +----------------------------------+ 4 rows in set (0.05 sec)
- Related Questions & Answers
- What is the easiest way to store date in MySQL database?
- Fastest way to search for a date from the date records in MySQL
- The easiest way to concatenate two arrays in PHP?
- Easiest way to get number of rows in a MySQL table?
- Easiest way to sort an array in MongoDB
- Insert current date to the database in MySQL?
- What is the easiest way to lose weight without exercise?
- What is the easiest way to reverse a String in Java?
- Insert current time minus 1 hour to already inserted date-time records in MYSQL
- MySQL query to insert multiple records quickly
- What is the easiest way to convert int to string in C++
- Easiest way to copy values of one column to a new table in MySQL?
- Best way to change the date format in MySQL SELECT?
- MySQL - Insert current date/time?
- Insert records from multiple tables in MySQL
Advertisements