1

I have an ODBC connection, which simply queries a SQL table:

 SELECT * FROM [TABLE] WHERE myDate = ?

If I run the query in MS Query, it prompts for a date and I can just enter 4/2/2015 and it returns 4/2/2015 data perfectly

I have the parameter set to read from cell (B1):

=WorkSheetName!$B$1

When I switch back to Excel and put 4/2/2015 in B1 and then refresh - it gives me a conversion failed when converting date and/or time from character string error.

I tried editing my query to WHERE CONVERT(Varchar(10),myDate,101) = ? but had no luck. Not sure why I am getting this, seems like it should be so simple.

3 Answers 3

1

I appreciate the feedback I was getting - but it turned out to be something very simple on my part that I was overlooking. The actual cell I was keeping my date was formatted as a date, and giving a conversion error. Once I formatted it to a text cell, it returned the data properly for the given date. Thanks

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

Comments

0

The filter part must be padded with # while trying to use Date as a filter.

It should be like

SELECT * FROM [TABLE] WHERE myDate = #4/2/2015# 

3 Comments

Thanks for the response but where shoudl the # #s go? If I put them in my command text (SELECT * FROM TABLE WHERE myDate= #?# I get a slew of errors (invalid column name '#@P1#' , Statement(s) could not be prepared / The following data range failed to refresh" etc...
Hi Saagar I have no VBA code. I tried putting a form button in to worksheets.refreshall at one point but that didnt work so I got rid of it. Its all being done through the connection definition tabs
0

Converting the "myDate" column to "Smalldatetime" format should work for you.

Try this:

SELECT * FROM [TABLE] WHERE Cast(myDate as smalldatetime) = ?

Thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.