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 ?
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.
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.
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;
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.
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 ...
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.
Use the DateValue()
function to convert a string
to date
data type. That's the easiest way of doing this.
DateValue(String Date)
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With