0

I'm trying to query a CSV via ADODB/SQL and the results need to be ordered by date. The challenge is that the date is in dd-mon-yy format. I tried to use CONVERT and REPLACE but Excel 2003 keeps throwing Automation Error at me. I've tried both of the following separately.

With CONVERT:

"SELECT CONVERT(datetime,[Business Date],106) from [filename.csv]"

With REPLACE:

"SELECT REPLACE([Business Date],'-',' ') from [filename.csv]"

I don't have control over the CSV so manually correcting the dates is not an option.

1 Answer 1

1

How about:

 SELECT CDate(Right([Business Date],2) & "/" _
    & Mid([Business Date],4,3) & "/" _
    & Left([Business Date],2)) from [filename.csv]

Depending, of course, on your locale. I do not believe Convert and Replace are available to ADO.

Sign up to request clarification or add additional context in comments.

3 Comments

You're missing the closing bracket for CDATE. The following worked for me. Thanks. SELECT CDate(Left([Business Date],2) & "/" & Mid([Business Date],4,3) & "/" & Right([Business Date],2)) from [filename.csv]
@fasih.ahmed Another thought, with yy format you may run into problems with ambiguous dates. It may be best to add another couple of digits to the year, if you can.
Good suggestion but this is one of those big corporations thing where getting this changed would take atleast a year. If I was controlling the data, I would format it yyyy.mm.dd because in that case sorting based on lexical order or unixtime would produce the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.