Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to create a reporting spreadsheet that can print reports for a given time period. The query below works, but when I try to use a ? parameter in place of the date, I get an error after selecting a cell containing my date. If I use single quotes ('?') I get a conversion from string to date/time failure, if I don't (?) I get a syntax error near @p1.

Eventually I will need either a start and end date or a formula adding a month or shift to the starting date/time to filter the data down to important information. The query was built in Microsoft Query.

SELECT FloatTable.DateAndTime,
       TagTable.TagName
FROM   master.dbo.FloatTable FloatTable,
       master.dbo.TagTable TagTable
WHERE  FloatTable.TagIndex = TagTable.TagIndex
       AND (( FloatTable.DateAndTime >= {ts '2012-06-01 00:00:00'} )) 

Any assistance would be much appreciated.

share|improve this question

1 Answer 1

You'll need to enter the query as a formula instead of as text, and build the date from the cell you've selected so that a valid sql statement is created.

Assuming you have a date in cell A1, put the following formula in cell B1:

="SELECT FloatTable.DateAndTime, TagTable.TagName FROM master.dbo.FloatTable _ 
FloatTable, master.dbo.TagTable TagTable WHERE FloatTable.TagIndex = _
TagTable.TagIndex AND ((FloatTable.DateAndTime>={ts '" & YEAR(A1) & "-" _
& MONTH(A1) & "-" & DAY(A1) & " 00:00:00'}))"

Note that the underscores at the end of each line indicate continuation. You'll need to delete them and the carriage returns for the formula to work. The entire formula is set up on a single row below. (Hard to read on this site, but Excel likes it!)

="SELECT FloatTable.DateAndTime, TagTable.TagName FROM master.dbo.FloatTable FloatTable, master.dbo.TagTable TagTable WHERE FloatTable.TagIndex = TagTable.TagIndex AND ((FloatTable.DateAndTime>={ts '" & YEAR(A1) & "-" & MONTH(A1) & "-" & DAY(A1) & " 00:00:00'}))"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.