简体   繁体   中英

OpenRowset filter query results by date

i am trying to filter some querys by date. i am using OPENROWSET to extract data from DBF tables but i can not filter by date, Visual Studio says that the data type is Database Date [DT_DBDATE]. I am using this query:

SELECT * FROM
OPENROWSET(
'VFPOLEDB', 
'C:\FOLDER\VFPDB\TABLES' 
;'';'', 
'SELECT *
FROM pedidoc
WHERE date BETWEEN '20120301' and '20120330''
) AS pedidoc

but sql server says 'incorrect syntax neat '20120301' any idea how to filter inside OPENROWSET?

You will need to use double-quotes for your filter and use the date format with - between the date parts.

SELECT * FROM
OPENROWSET(
'VFPOLEDB', 
'C:\FOLDER\VFPDB\TABLES' 
;'';'', 
'SELECT *
FROM pedidoc
WHERE date BETWEEN ''2012-03-01'' and ''2012-03-30'' '
) AS pedidoc

Inside OPENROWSET, SELECT statement must be of syntax acceptable by OLEDB provider. In this case, use not SQL SERVER syntax, but VISUAL FOXPRO syntax:

SELECT * FROM
OPENROWSET(
'VFPOLEDB', 
'C:\FOLDER\VFPDB\TABLES' 
;'';'',
'SELECT * FROM pedidoc WHERE BETWEEN(date, {^2012-03-01}, {^2012-03-30})'
) AS pedidoc

There might be problem with date/time type conversion from VFP->MSSQL therefore convert them to string using DTOS function (date to string):

'SELECT DTOS(date) as datestr FROM pedidoc WHERE BETWEEN(date, {^2012-03-01}, {^2012-03-30})'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM