Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Date in MS Access Query

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 am trying to cast my value, which is a string using Format &CDate function with datetime data type and it throws out as Overflow.

Here's query :

Select * from Events
Where Events.[Date] > cDate(Format("20130423014854","yyyy-MM-dd hh:mm:ss"))

Sample Date Record Value from Table : 2013-04-23 13:48:54.0

Events.[Date] is a Date/Time type field in access

How can I fix this ?

like image 574
user2385057 Avatar asked Jun 28 '13 09:06

user2385057


People also ask

How do I format a date in an Access query?

Access provides several predefined formats for date and time data. Open the table in Design View. In the upper section of the design grid, select the Date/Time field that you want to format. In the Field Properties section, click the arrow in the Format property box, and select a format from the drop-down list.

What is CDate in MS Access?

CDate* Converts text to a Date/Time value. Handles both the Date and Time portion of the number. Tip: Use the BooleanIsDate function to determine if a text string can be converted to a Date/Time value.

How to convert date time string to date in MS Access?

Convert date time string to date. MS Access Table January2015 has a txndate field with the string "2015-01-01 11:48:00". The field type is text. The string needs to be converted to date/time i.e. it should appear in the same format but as a time. Running this query: SELECT Format (datevalue (txndate), "dd-mm-yyyy hh:mm:ss") FROM January2015;

What is the syntax for datevalue function in MS Access?

The syntax for the DateValue function in MS Access is: A string representation of a date. The string_date can be a date ranging from January 1, 100 to December 31, 9999. It can include a time component, if desired. The DateValue function returns a date value.

How to convert string to date in SQL Server?

In SQL Server, converting string to date implicitly depends on the string date format and the default language settings (regional settings); If the date stored within a string is in ISO formats: yyyyMMdd or yyyy-MM-ddTHH:mm:ss (.mmm), it can be converted regardless of the regional settings, else the date must have a supported format ...

What is string_date in SQL?

A string representation of a date. The string_date can be a date ranging from January 1, 100 to December 31, 9999. It can include a time component, if desired. The DateValue function returns a date value.


2 Answers

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

DateValue(String Date) 
like image 121
Diego dot net Avatar answered Oct 25 '22 03:10

Diego dot net


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'
like image 26
Gord Thompson Avatar answered Oct 25 '22 02:10

Gord Thompson