Question

I'm using an ADO\ACE.OLEDB query to access data in an excel sheet. One of the columns in this sheet is a time stamp in the 00:00:00 format.

My select max query for this column works OK but it returns the wrong "max" time. For example, if I have the following time stamps:
08:15:00
09:45:00
10:00:00

The SELECT MAX query will return 09:45:00, and ignore any subsequent timestamps. If I go to the queried sheet, and convert all the cells in that column to "Time" using text-to-columns, the query will then return the correct value of 10:00:00.

The timestamp that is recorded in the excel sheet is updated very often by many different people throughout the day using their own ADO Update\Insert query so it's not feasible for me to open the sheet and convert the cells to the correct type manually. The update\insert queries have the cell types set to General.

So my question is this. Is there a way to either
A) Specify the data type in the update\insert into ADO query so the cell has the correct data type? Or
B) Force the SELECT MAX query to interpret the column as a timevalue rather than text?

If not any suggestions on how to procede?

EDIT: I looked closer at the excel sheet i'm querying. I noticed that each cell has a singlequote preceding it. I think if I can get that removed when entries are recorded excel might properly format the cell as time. Here is a basic example of wha is being used to write entries to the sheet. Update [Sheet1$] Set StopTime='09:45:00' WHERE Date= '3/12/2014' AND StartTime='9:30:00'

Can this be rewritten so a single quote ins't included? Note there is no end quote.

Was it helpful?

Solution

The single quote at the beginning of a cell in Excel forces Excel to interpret the cell value as text. The single quote is probably being inserted because the update statement uses single quotes, which indicate strings. You may be able to use ## delimiters instead, which specifically indicate date/time values (I say "may" because I haven't tested this). For example:

UPDATE [Sheet1$]
SET StopTime = #09:45:00#
WHERE [...]

On the other side, you can force a column to be interpreted as a date/time by using the CDate function:

SELECT MAX(CDate(StopTime)) AS MaxTime
FROM [Sheet1$]

You just have to be careful in this case about values that cannot be converted to a valid time, which will cause the query to error out.

OTHER TIPS

I didn't even realise this but the examples I gave were flawed. What is logged does NOT include a leading zero. Since I'm using a 24hr time I was able to correct this issue by writing a leading zero into the time that gets recorded.

Basically it's an, if LEN(time)=7 then time = 0 & time.

This appears to be working so far.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top