Select from MS Access Table between two dates?

最后都变了- 提交于 2019-12-24 07:27:09

问题


I have an MS ACCESS Table with a string field i use to store dates (I have my reasons to not use a type Date), Is there a way to select rows from the table between two dates ? cuz everything i tried doesnt seem to work, it confuses years, days and months, here is what i tried :

select  *  from audience where Format(auddate, "dd/MM/yyyy") between #01/06/2014# and  #01/08/2014#
select  *  from audience where Format(auddate, "dd/MM/yyyy") > #01/06/2014# and Format(auddate, "dd/MM/yyyy") > #01/08/2014#

among others and i get some meaningless results :

AudDate
25/06/2014
18/09/2012
12/11/2012
28/01/2013
08/02/2011
13/10/2011

Thanks in advance.


回答1:


Try CDate() to convert your string into a date.

select  *  from audience 
where CDate(audate) between #01/06/2014# and #01/08/2014#;

If it doesn't work because CDate does not reconize your format you can use DateSerial(year, month, day) to build a Date. You will need to use mid$ and Cint() to build the year, month and day arguments. Something like this for a format "yyyy-mm-dd":

DateSerial(CInt(mid(audate, 1, 4)), CInt(mid(audate, 6, 2)), CInt(mid(audate, 9, 2))

Hope this helps.



来源:https://stackoverflow.com/questions/24622282/select-from-ms-access-table-between-two-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!