
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Perform MySQL Select on Dates Stored as Varchar
Let us first create a table −
mysql> create table DemoTable ( DueDate varchar(100) ); Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('21/10/2018'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('18/08/2019'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('01/12/2012'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('31/01/2016'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+ | DueDate | +------------+ | 21/10/2018 | | 18/08/2019 | | 01/12/2012 | | 31/01/2016 | +------------+ 4 rows in set (0.00 sec)
Following is the query to perform SELECT on dates included in the above table as VARCHAR. Here, we are fetching all the date records after the date 2010-01-31 −
mysql> select *from DemoTable where str_to_date(DueDate,'%d/%m/%Y') > '2010-01-31';
This will produce the following output −
+------------+ | DueDate | +------------+ | 21/10/2018 | | 18/08/2019 | | 01/12/2012 | | 31/01/2016 | +------------+ 4 rows in set (0.00 sec)
Advertisements