
- 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
Create date from day, month, year fields in MySQL?
You can use in-built function STR_TO_DATE() from MySQL. The syntax is as follows −
SELECT STR_TO_DATE(CONCAT(yourYearColumName,'-',LPAD(yourMonthColumName,2,'00'),'-',LPAD(yourDayColumName,2,'00')), '%Y-%m-%d') as anyVariableName from yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table DateCreateDemo −> ( −> `Day` varchar(2), −> `Month` varchar(2), −> `Year` varchar(4) −> ); Query OK, 0 rows affected (1.68 sec)
Insert values for all fields using insert command. The query is as follows −
mysql> insert into DateCreateDemo values('15','12','2018'); Query OK, 1 row affected (0.09 sec) mysql> insert into DateCreateDemo values('10','11','2016'); Query OK, 1 row affected (0.37 sec) mysql> insert into DateCreateDemo values('12','6','2017'); Query OK, 1 row affected (0.12 sec) mysql> insert into DateCreateDemo values('9','8','2019'); Query OK, 1 row affected (0.17 sec) mysql> insert into DateCreateDemo values('10','5','2020'); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from DateCreateDemo;
The following is the output −
+------+-------+------+ | Day | Month | Year | +------+-------+------+ | 15 | 12 | 2018 | | 10 | 11 | 2016 | | 12 | 6 | 2017 | | 9 | 8 | 2019 | | 10 | 5 | 2020 | +------+-------+------+ 5 rows in set (0.00 sec)
Create a date using the above three fields which is ‘Day’,’Month’ and ‘Year’. The query is as follows −
mysql> select −> STR_TO_DATE(CONCAT(Year,'-',LPAD(Month,2,'00'),'-',LPAD(day,2,'00')), '%Y-%m-%d') as DateDemo from DateCreateDemo;
The following is the output −
+------------+ | DateDemo | +------------+ | 2018-12-15 | | 2016-11-10 | | 2017-06-12 | | 2019-08-09 | | 2020-05-10 | +------------+ 5 rows in set (0.06 sec)
- Related Articles
- Finding day of week from date (day, month, year) in JavaScript
- Format MySQL date and convert to year-month-day
- How to get a Date from year, month and day in Java?
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- Fetch month, day, year, etc. from ISODate in MongoDB?
- How to Convert Numbers to Year/Month/Day or Date in Excel?
- Combine three strings (day, month, year) and calculate next date PHP?
- How can we extract the Year and Month from a date in MySQL?
- How to convert year, month, and day of the month into a complete date in R?
- How to display first day and last day of the month from date records in MySQL?
- How can we fetch month and day from a given date in MySQL?
- Filter the records of current day, month and year in MySQL?
- MySQL query to fetch date with year and month?
- Display month names and year from a column with date records with MySQL
- Compare only day and month with date field in MySQL?

Advertisements