Here's the query that is being executed on SQL Server 2008:
SELECT "dbo"."tblMainData"."recID"
FROM "dbo"."tblMainData"
WHERE ("timeInMain" BETWEEN {t '08:00:00'} AND {t '17:00:00'} )
This query is generated by a Microsoft Access 2007 database that has linked tables in the SQL Server 2008 database.
Here is the Access query that generates the above query:
SELECT tblMainData.timeInMain
F开发者_Go百科ROM tblMainData
WHERE (((tblMainData.timeInMain) Between #12/30/1899 8:0:0# And #12/30/1899 17:0:0#));
Anyway, I know that there is data that meets this criteria. Why aren't I seeing the expected results?
This is the first time I have seen the {t '08:00:00'}
syntax, which I'm assuming is supposed to disregard the date portion of the datetime field. I couldn't find any documentation on it.
Thanks for any help.
UPDATE
Just ran select {t '08:00:00'}
in SSMS, which returned 2010-11-17 08:00:00.000
. That explains why I'm not getting data back. But, as you can see, the Access query is explicit about which date to use and the generated TSQL doesn't include it. What to do?
UPDATE 2
I have done some experimenting. Access only removes the date from generated TSQL when it is 12/30/1899. Apparently, this is a special date that access uses when a user stores only a time in a DateTime field and Access assumes that if it's included in a query, then it's not really important. Wow.
So, I believe my options are:
- Call stored procedures from Access. I'm not sure how to do this yet.
- Update the data in the sql server database with a date that wont be discarded on TSQL generation.
- Try to find a way to make the generated TSQL include the explicit 12/30/1899 date. IDEAL
Any other ideas?
You can change your where clause to the following which removes the date on the left side of the comparison
WHERE TimeValue(tblMainData.timeInMain) Between #12/30/1899 9:0:0# And #12/30/1899 17:0:0#
If you know that all values in the database will be on 12/30/1899 than you can just do
WHERE tblMainData.timeInMain Between #12/30/1899 9:0:0# And #12/30/1899 17:0:0#
As an aside if you want to do this in SQL Server Procedure you can strip the date using the following (its not much slower than if you knew the dates)
WHERE
DATEADD( DAY , -1 *DATEDIFF(DAY, 0, timeInMain ) , timeInMain )
BETWEEN '09:00' and '17:00'
UPDATE
Here's how I tested it
I created a table and populated it using the following in SQL Server
CREATE Test
(
id int identity,
DateTimeValue DateTime
)
--Values with COM Zero Date
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 8:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 9:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 10:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 11:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 12:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 1:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 2:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 3:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 4:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 5:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 6:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1899 7:00 PM')
--Values with SQL Zero Date
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 8:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 9:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 10:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 11:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 12:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 1:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 2:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 3:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 4:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 5:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 6:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/1900 7:00 PM')
--Abritrary Date After Zero Dates
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 8:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 9:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 10:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 11:00 AM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 12:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 1:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 2:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 3:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 4:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 5:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 6:00 PM')
INSERT INTO Test (DateTimeValue) VALUES ('1/1/2010 7:00 PM')
I created a ODBC link to this table in MS Access 2007
And then created the following Query which results in the 27 records that have a time value between 9:00 AM and 5:00 PM
SELECT id, DateTimeValue
FROM dbo_Test
WHERE TimeValue(DateTimeValue) Between #12/30/1899 9:0:0# And #12/30/1899 17:0:0#
精彩评论