
- 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
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 Articles
- What is the easiest way to store date in MySQL database?
- Fastest way to search for a date from the date records in MySQL
- Easiest way to get number of rows in a MySQL table?
- Insert current time minus 1 hour to already inserted date-time records in MYSQL
- The easiest way to concatenate two arrays in PHP?
- Insert current date to the database in MySQL?
- Easiest way to sort an array in MongoDB
- Easiest way to copy values of one column to a new table in MySQL?
- MySQL query to insert multiple records quickly
- Insert records from multiple tables in MySQL
- How to express the number16000 in exponential form in the easiest way?
- How to insert records with double quotes in MySQL?
- What is the easiest way to reverse a String in Java?
- What is the easiest way to convert int to string in C++
- What is the easiest way to lose weight without exercise?

Advertisements