I am storing my dates in column server_date_time in varchar
in dd/mm/yyyy
format and i want to fetch the records lying between some dates so i have used the query
select * from activity_emp
where date_format(str_to_date(substr(server_date_time,1,10),'%d/%m/%Y'),'%d/%m/%Y')>=
'29/09/2012'
and date_format(str_to_date(substr(server_date_time,1,10),'%d/%m/%Y'),'%d/%m/%Y')<=
'07/10/2012';
I have converted varchar
to string in query but my query return query data only related to 29/09/2012 and 30/09/2012. It should also return query for the month of October
This is Simple and Easy to understand Date Query to search between Two Dates
If you are storing the dates always in
dd/mm/yyyy
in the column then this is an easy task:Isn't this enough? Hope it helps you
Try this one -
But it is better to store
server_date_time
in DATETIME data type so that MySQL can use index.Try with this. You can input date in dd/mm/yyyy format as in your question...
Update: I strongly recommend you to change datatype from
VARCHAR
toDATETIME
Cheers!!!
STR_TO_DATE
is enough.DATE_FORMAT
changes it back toVARCHAR
when dealing
date
please useDATE
orDATETIME
data type. This will avoid you from doing casting which affects the performance of the query.