简体   繁体   中英

Date and time stamp comparison with today in ODBC in Excel?

Question

I'm querying a DataBase via Excel ODBC.

I can compare the field with an hard-coded date in my Excel's ODBC query with :

BOMD.BOMENDDAT_0 > {ts '2018-05-05 00:00:00'}

But I can't seem to make a proper comparison with the present date :

BOMD.BOMENDDAT_0 > {ts SYSDATETIME()}
DATEDIFF(d, SYSDATETIME(), BOMD.BOMENDDAT_0) > 0

Any suggestion ?


Context

I'm trying to build a query that select only records where the date of today is between :

  • a start date ( BOMD.BOMSTRDAT_0 )
  • an end date ( BOMD.BOMENDDAT_0 )

Those dates :

  • are in a special format, they look like this in Excel : '26/02/2018 00:00:00'
    (French format, Day Month Year )
  • can be filled or not
  • 0 seems to be '00/01/1900 00:00:00'

The whole query look like this :

SELECT BOMD.BOMQTY_0, BOMD.UPDDAT_0, BOMD.BOMSTRDAT_0, BOMD.BOMENDDAT_0
FROM myDB.BOMD BOMD
WHERE BOMD.UPDDAT_0 > {ts '2018-01-01 00:00:00'}
AND (
    (BOMD.BOMSTRDAT_0 < {ts SYSDATETIME()})
    OR BOMD.BOMSTRDAT_0 = 0
    OR BOMD.BOMSTRDAT_0 is null
    )
AND (
    (BOMD.BOMENDDAT_0 > {ts SYSDATETIME()})
    OR BOMD.BOMENDDAT_0 = 0
    OR BOMD.BOMENDDAT_0 is null
    )

{ts '2018-05-05 00:00:00'} is used to convert string into datetime data type, you cannot use it on SYSDATETIME() which is already a datetime (precisely datetime2) data type, just make it:

SELECT BOMD.BOMQTY_0, BOMD.UPDDAT_0, BOMD.BOMSTRDAT_0, BOMD.BOMENDDAT_0
FROM myDB.BOMD BOMD
WHERE BOMD.UPDDAT_0 > {ts '2018-01-01 00:00:00'}
AND (
    (BOMD.BOMSTRDAT_0 < SYSDATETIME())
    OR BOMD.BOMSTRDAT_0 = 0
    OR BOMD.BOMSTRDAT_0 is null
    )
AND (
    (BOMD.BOMENDDAT_0 > SYSDATETIME())
    OR BOMD.BOMENDDAT_0 = 0
    OR BOMD.BOMENDDAT_0 is null
    )

btw, your code will also return rows where both BOMENDDAT_0 and BOMSTRDAT are zeros or nulls, is it really intended behaviour?

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