MS Flow:SQL 服务器 Get_rows - 我们不能将运算符 < 应用于类型 DateTimeZone 和 DateTime

MS Flow: SQL Server Get_rows - We cannot apply operator < to types DateTimeZone and DateTime

问题

我正在尝试编写一个简单的流程,它从我的 SQL 服务器数据库中收集一些行并通过电子邮件将数据发送给我。但是,我无法按日期过滤这些记录。我希望 Flow 仅检索过去一天的记录,因此我正在尝试执行以下 Odata 过滤器查询之类的操作:

ItemCreatedWhen gt addDays(utcNow('yyyy-MM-ddTHH:mm:ssZ'), -1)

当我 运行 但是,我收到以下错误消息:

We cannot apply operator < to types DateTimeZone and DateTime.

该列的SQL服务器数据类型为'yyyy-MM-dd HH:mm:ss'

形式的datetime2

我尝试过的

我试过以下方法:

ItemCreatedWhen gt addDays(utcNow(), -1)

错误:我们不能将运算符 < 应用于类型 DateTimeZone 和 DateTime。

ItemCreatedWhen gt convertFromUtc(addDays(utcNow(), -1), 'Eastern Standard Time')

Error: The DateTimeOffset text '2019-01-24T10:59:25.7848207' should be in format 'yyyy-mm-ddThh:mm:ss('.'s+)?(zzzzzz)?' and each field value is within valid range. inner exception: The time zone information is missing on the DateTimeOffset value '2019-01-24T10:59:25.7848207'. A DateTimeOffset value must contain the time zone information.

假设您的 table 中的列 "ItemCreatedWhen" 是 date/time 格式,只需将该查询发送到 SQL 服务器以获取昨天的数据:

SELECT * FROM YourTable
WHERE ItemCreatedWhen > DATEADD(DAY,-2,CAST(GETUTCDATE() as DATE)) 
    and ItemCreatedWhen < CAST(GETUTCDATE() as DATE)

我们最近遇到了 PostgreSQL 连接器的这个问题(这通常是 Power Automate/Logic 应用程序中一般数据库连接器的问题)。

我的同事写了一个blog post about this and what has to be done if you want to filter by DateTime. Generally, the problem is, that the Power Query implementation (the thing which executes the query against the database) doesn't properly work with DateTime/DateTimeZone and similar fields (this is further described in SQL Server Connector的限制,它也适用于其他数据库连接器(如 PostgreSQL)。

解决方案是使用 yearmonthdayhourminute 函数将 DateTime 值转换为数字,您可以然后用来比较。这导致必须执行一个非常具有挑战性的查询,因为您不能简单地调用:

mytime = "2020-12-31T10:00:00Z`
year(dbfield) ge year(mytime) and month(dbfield) ge month(mytime) and day(dbfield) ge day(mytime)

由于直接的月份比较会排除 1 到 11 月以及相同的日期,排除 1 到 30,因此这会省略很多值。

为了防止这种情况,您必须进行更复杂的查询(我对其进行了格式化,因此更具可读性):

(
    year(change) gt year(@{variables('Timestamp')})
    or
    (
        year(change) eq year(@{variables('Timestamp')})
        and
        (
            month(change) gt month(@{variables('Timestamp')})
            or
            (
                month(change) eq month(@{variables('Timestamp')})
                and
                (
                    day(change) gt day(@{variables('Timestamp')})
                    or
                    (
                        day(change) eq day(@{variables('Timestamp')})
                        and
                        (
                            hour(change) gt hour(@{variables('Timestamp')})
                            or
                            (
                                hour(change) eq hour(@{variables('Timestamp')})
                                and
                                (
                                    minute(change) ge minute(@{variables('Timestamp')})
                                )
                            )
                        )
                    )
                )
            )
        )
    )
)

我有一个与微软有关的票据,因为我真的很想 运行 定期查询日期比较,因为它会让我的生活更轻松(参考 #2111080010002309)。