I'm creating a new Logic App that reads a table where DateCreated < ADDDAYS(-60,GETDATE())
and updates an Archived
bit to 1
.
However, I can't for the life of me figure out how to implement that filter as part of the ODATA query.
Here's what I'm trying so far:
DateCreated lt addDays(utcNow(),-60)
However, I get "An unknown function with name 'utcnow' was found. This may also be a function import or a key lookup on a navigation property, which is not allowed.\r\n inner exception: An unknown function with name 'utcnow' was found. This may also be a function import or a key lookup on a navigation property, which is not allowed."
How can I filter on a dynamic date in the filer?
However, I can't for the life of me figure out how to implement that filter as part of the ODATA query.
I suppose you mean on the ODATA Query on the SQL Connector?
Can you try the following:
DateCreated lt @{addDays(utcNow(),-60)}
Based on the previous answer, you should try the same command :
DateCreated lt @{addDays(utcNow(),-60)}
But also, you must ensure that your data type, on the SQL side, must be a datetimeoffset.
Three solutions to do this :
Change the type of your field in your table,
Create a view and cast the field DateCreated to DATETIMEOFFSET
CREATE VIEW [dbo].[myview] AS
SELECT MyFields, ..., CAST(DateCreated AS DATETIMEOFFSET) AS DateCreated
FROM MyTable
Create a store procedure with a DATETIMEOFFSET parameter, and convert the parameter to a DATETIME
If you can not change your SQL code, this piece of code is the solution :
year(DateCreated) lt year(@{addDays(utcNow(),-60)}) or (
year(DateCreated) eq year(@{addDays(utcNow(),-60)}) and (
month(DateCreated) lt month(@{addDays(utcNow(),-60)} or (
month(DateCreated) eq month(@{addDays(utcNow(),-60)}
... <same thing for other date parts>
)
)
)
You have to compare each part of your date :
This is an interesting issue that sometimes shows up when dates,
times, datetimes, and specific time zone come into play. Comparing a
DateTimeZone to a date is problematic, because it might be less in
arithmetic terms but only if the time zone matches... without that
critical piece of information, these data types cannot be compared.
One alternative is to use the standard OData functions to retrieve
parts of the data type. For example:
$filter = year(release_date) lt year(dtz)
Of course, you must be carful to ensure that you are implementing the
correct logic with respect to timezone- but you are probably aware of
that.
OData Reference :
http://www.odata.org/documentation/odata-version-2-0/uri-conventions/