Convert String to Date in MS Access Query

后端 未结 5 1967
深忆病人
深忆病人 2020-12-06 16:41

I am trying to retrieve data from my access table based on Date column. My requirement is to display everything greater than the certain value. I a

5条回答
  •  盖世英雄少女心
    2020-12-06 17:20

    In Access, click Create > Module and paste in the following code

    Public Function ConvertMyStringToDateTime(strIn As String) As Date
    ConvertMyStringToDateTime = CDate( _
            Mid(strIn, 1, 4) & "-" & Mid(strIn, 5, 2) & "-" & Mid(strIn, 7, 2) & " " & _
            Mid(strIn, 9, 2) & ":" & Mid(strIn, 11, 2) & ":" & Mid(strIn, 13, 2))
    End Function
    

    Hit Ctrl+S and save the module as modDateConversion.

    Now try using a query like

    Select * from Events
    Where Events.[Date] > ConvertMyStringToDateTime("20130423014854")
    

    --- Edit ---

    Alternative solution avoiding user-defined VBA function:

    SELECT * FROM Events
    WHERE Format(Events.[Date],'yyyyMMddHhNnSs') > '20130423014854'
    

提交回复
热议问题