简体   繁体   中英

retrieve access records that fall between user specified dates

The table has a date field that is text. The SQL statement is:

"SELECT datefield, anotherfield FROM tablename WHERE CDate(datefield) BETWEEN #" & dateStart & "# AND #" & dateEnd & "#"

dateStart and dateEnd are strings, like "10/02/2017" and "10/4/2017". I used CDate to convert the string datefield to a date, and the bracketing # around the start and end date strings so that they will be treated as date. I have tried, literally, dozens of different variants of the WHERE clause with no luck. Any suggestions are appreciated.

I certainly agree that dates should not be stored as text. However, if you are stuck with the table design then you will need to use CDate for all three of your "date" fields:

SELECT CDate([datefield]) AS myDate, anotherfield
FROM Table2
WHERE (((CDate([datefield])) Between CDate([dateStart]) And CDate([dateEnd])));

I've also used your ways in storing and retrieving date in mySQL. However, I only used one field instead of your perspective dateStart and dateEnd . I would suggest you only create one field for storing date. Here's how I managed to catch the values between those dates using VB.NET.

SELECT datefield, anotherfield FROM tablename WHERE datestoredfield BETWEEN '" & selectedDateFrom.toString("MM/dd/yyyy") & "' AND '" & selectedDateTo.toString("MM/dd/yyyy") & "';

I've indicated .toString("MM/dd/yyyy") at the end of the selected dates its because your current stored date format in your date field is MM/dd/yyyy .

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