简体   繁体   English

在Excel中的ODBC中与今天的日期和时间戳进行比较?

[英]Date and time stamp comparison with today in ODBC in Excel?

Question

I'm querying a DataBase via Excel ODBC. 我正在通过Excel ODBC查询数据库。

I can compare the field with an hard-coded date in my Excel's ODBC query with : 我可以使用以下命令Excel的ODBC查询中 将该字段与硬编码日期进行比较

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 ) 开始日期( BOMD.BOMSTRDAT_0
  • an end date ( BOMD.BOMENDDAT_0 ) 结束日期( BOMD.BOMENDDAT_0

Those dates : 那些日期:

  • are in a special format, they look like this in Excel : '26/02/2018 00:00:00' 格式特殊,在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' 0似乎是'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: {ts '2018-05-05 00:00:00'}用于将字符串转换为日期时间数据类型,您不能在已为日期时间(精确为datetime2)数据类型的SYSDATETIME()上使用它,只需使其:

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? 顺便说一句,您的代码还将返回BOMENDDAT_0BOMSTRDAT均为零或为null的行,这真的是预期的行为吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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