I have a database(table), in which 2 fields are:
fromdate varchar(20) todate varchar(20)
Dates are stored in this fashion:
YYYY-MM-DD HH:mm:ss
For ex: ‘2014-10-30 10:10:10’ in database.
Now I want to compare two dates and fetch records from database by using query, 2014-09-10 10:10:10(fromdate
) to 2014-10-10 10:10:10(todate
)
How to fetch all accurate records.. Is there any kind of solution..
Thanks.
Advertisement
Answer
Just compare the string without extra overhead.
This format “YYYY-MM-DD HH:mm:ss” shares chronological and literal alphabetical order
SELECT * FROM someTable WHERE fromdate >= '2014-09-10 10:10:10' AND todate <= '2014-10-10 10:10:10'
Also, I would create an index on those columns.