- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set format specifier in MySQL STR_TO_DATE() and convert string to date
Specify format specifier in STR_TO_DATE() method that convertes string to date. Following is the syntax −
select STR_TO_DATE(yourColumnName,'yourFormatSpecifier') from yourTableName;
Let us first create a table −
mysql> create table DemoTable -> ( -> AdmissionDate varchar(100) -> ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('01-12-2019'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('15-06-2019'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('21-01-2016'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+---------------+ | AdmissionDate | +---------------+ | 01-12-2019 | | 15-06-2019 | | 21-01-2016 | +---------------+ 3 rows in set (0.00 sec)
Following is the query for STR_TO_DATE(). Here, we have set format specifier as well −
mysql> select STR_TO_DATE(AdmissionDate,'%d-%m-%Y') from DemoTable;
Output
This will produce the following output −
+---------------------------------------+ | STR_TO_DATE(AdmissionDate,'%d-%m-%Y') | +---------------------------------------+ | 2019-12-01 | | 2019-06-15 | | 2016-01-21 | +---------------------------------------+ 3 rows in set (0.04 sec)
Advertisements