Comparing dates from SQL Server linked table in Access

三世轮回 提交于 2019-12-08 06:22:30

问题


I have a SQL Server linked table in Access and I am trying to query to extract from a certain date or data ranges (i.e. < 01/31/2017, for example). I tried in the criteria in Access design mode, the following: < #01/31/2017# but it is not comparing correctly.

The field in the SQL server table is datetime data type, I formatted it in Access as Short Date in the properties in design mode.

SELECT dbo_LicensesLiveViewWithRevenue.BAN, 
       dbo_LicensesLiveViewWithRevenue.PTN, 
       dbo_LicensesLiveViewWithRevenue.SOC, 
       dbo_LicensesLiveViewWithRevenue.LicenseCreatedOn, 
       Format([DeactivationDate],"Short Date") AS DeactOn, 
       dbo_LicensesLiveViewWithRevenue.RetailPrice, 
       dbo_LicensesLiveViewWithRevenue.WholeSalePrice, 
       dbo_LicensesLiveViewWithRevenue.AccountID
FROM dbo_LicensesLiveViewWithRevenue
WHERE (((dbo_LicensesLiveViewWithRevenue.LicenseCreatedOn)< #2017/01/31#)) ORDER BY dbo_LicensesLiveViewWithRevenue.LicenseCreatedOn;

I also tried the query with passing the dates as parameters from a form, with no luck.

SELECT dbo_LicensesLiveViewWithRevenue.BAN, 
       dbo_LicensesLiveViewWithRevenue.PTN, 
       dbo_LicensesLiveViewWithRevenue.SOC, 
       dbo_LicensesLiveViewWithRevenue.LicenseCreatedOn, 
       Format([DeactivationDate],"Short Date") AS DeactOn, 
       dbo_LicensesLiveViewWithRevenue.RetailPrice, 
       dbo_LicensesLiveViewWithRevenue.WholeSalePrice, 
       dbo_LicensesLiveViewWithRevenue.AccountID 
FROM dbo_LicensesLiveViewWithRevenue
WHERE (((dbo_LicensesLiveViewWithRevenue.LicenseCreatedOn)<[Forms]![MainForm]![EndDate]));

The results:

When I run the query directly in SQL server is giving me the correct records with correct dates (in SQL the dates are < '20170131'). In Access I have the same count of records but the dates are showing wrong as in the result pictures.


回答1:


I would suggest that you open the linked table in design view. Ignore the prompt msg about this action being read only).

If the columns in question are newer date time formats (datetime2), and you link using the standard SQL driver, then such date are seen as TEXT columns, and NOT date/time.

A quick look at the linked table in design view will revel if the columns are seen by access as date or text columns.

If you are using any newer format dates from SQL server, you MUST use the newer native 11 (or later) drivers when you link tables to SQL server. It is for this reason that I tend to stick with the older “default” legacy SQL driver. The legacy drivers are installed and included with windows - and thus no install of drivers is required when you run Access on each workstation.

However, if you are using newer date formats in SQL server, then you need to link the tables using the native 11 drivers, and you ALSO have to distribute and install the native 11 drivers on each workstation in addition to Access (or the Access runtime). So while one should prefer and use the newer SQL drivers, the downside is these drivers have to be installed on each workstation.

So double check the data type for those columns that Access shows in table design mode – if they are text, then you need to re-link with the native 11 drivers.



来源:https://stackoverflow.com/questions/46331831/comparing-dates-from-sql-server-linked-table-in-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!