Convert String to Date in MS Access Query

后端 未结 5 1956
深忆病人
深忆病人 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

    Use the DateValue() function to convert a string to date data type. That's the easiest way of doing this.

    DateValue(String Date) 
    
    0 讨论(0)
  • 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'
    
    0 讨论(0)
  • 2020-12-06 17:27

    Basically, this will not work out

    Format("20130423014854","yyyy-MM-dd hh:mm:ss")
    

    the format function will only work if your string has correct format

    Format (#17/04/2004#, "yyyy/mm/dd")
    

    And you need to specify, what datatype of field [Date] is, because I can't put this value 2013-04-23 13:48:54.0 under a General Date field (I use MS access2007). You might want to view this topic: select date in between

    0 讨论(0)
  • 2020-12-06 17:31
    cdate(Format([Datum im Format DDMMYYYY],'##/##/####') ) 
    

    converts string without punctuation characters into date

    0 讨论(0)
  • 2020-12-06 17:39

    If you need to display all the records after 2014-09-01, add this to your query:

    SELECT * FROM Events
    WHERE Format(Events.DATE_TIME,'yyyy-MM-dd hh:mm:ss')  >= Format("2014-09-01 00:00:00","yyyy-MM-dd hh:mm:ss")
    
    0 讨论(0)
提交回复
热议问题