Reputation: 327
HI,
I have this string: "2010-12-27 23:05:36.0". When I parse it with CDate like this: CDate("2010-12-27 23:05:36.0") it returns error 13. When I remove the '.0' from the string it is working fine.
The date string comes from a database where there is a mixture of dates formatted like this: 'yyyy-mm-dd' and 'yyyy-mm-dd hh-MM-ss.n' What would be the easy way to get rid of the error 13?
Upvotes: 1
Views: 1561
Reputation: 131571
The best solution is to NOT store dates as strings. All databases can handle dates and ADO can return date values directly without converting them to strings. If the date data is stored as a date type you probably don't need to use CDate at all. The returned recordset will contain the underlying data as a Date variant.
If the data is stored as a string after all, you could possibly modify your SQL statement to convert the string to a date using your DB's built-in functions. In SQL Server you can do that with the CAST and CONVERT functions, e.g.
CAST(myDateAsTextField as datetime)
SQL Server's DATETIME
data type accepts fractional seconds with 1 to 3 digits.
Finally, you can check the contents of the string and convert it to a different form before calling CDate. This should be used as a last resort if you can't fix the data at the database level.
Upvotes: 5
Reputation: 2251
You could check the string length -> if it is >19, get rid of the rest and apply then the CDate function
Upvotes: 1