简体   繁体   中英

Records between start and end date

I am having two textfields which represents startDate and endDate.Now the problem I am facing is that I want all the records from the database which occured between this interval.But the field which stores date is TimeStamp and is of format :

24-APR-14 09.23.44.458000 PM or we can say dd-mm-yy hh.min.ss.milli AM/PM

Now obviously user entering the date is not going to enter it in such a format.So what should be query to select records from table say t1 between this date interval.

Saving Date data as Date or Datetime makes life easy. You have tagged Mysql in the question so here is a Mysql Solution.

This is what you can do

select * from
test
where
date_format(str_to_date(`date`,'%d-%b-%y'),'%Y-%m-%d') 
between '2014-04-15' AND '2014-04-24'

DEMO

You can format the user input as you want in the query in DATE_FORMAT().

User input will have a startdate and a enddate may be in datetime OR date variable

while trying to use these two input variables in the WHERE CLAUSE use BETWEEN CAST(@StartDate AS DATE) AND (CAST(@EndDate AS DATE) + 1)

This will fetch results that lies in both dates and the data range as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM