OData query filter for Datatime

Mohamed Hussein 530 Reputation points
2024-12-08T06:07:37.1366667+00:00

Good Day,

At Azure Table Storage, if i want to filter by columns less than Now()

I tried the below but aint work, using Storage Exporer and Python

"ExpiresOn lt datetime'2024-12-08T06:06:00.976Z'"

      now = datetime.now(timezone.utc)
        formatted_now = now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
        my_filter =  f"ExpiresOn lt datetime'{formatted_now}'"
        entities = table_client.query_entities(my_filter)

Image 2

Image 3

Azure Table Storage
Azure Table Storage
An Azure service that stores structured NoSQL data in the cloud.
172 questions
0 comments No comments
{count} votes

Accepted answer
  1. Vinodh247 26,371 Reputation points MVP
    2024-12-08T13:47:12.3966667+00:00

    Hi,

    Thanks for reaching out to Microsoft Q&A.

    When filtering DateTime values in Azure Table Storage using OData queries, it's essential to ensure that the DateTime string is correctly formatted and that the column names are accurate. Here's how you can approach this:

    1. Verify the DateTime Format: Azure Table Storage expects DateTime values in the ISO 8601 format with milliseconds and a 'Z' suffix to indicate UTC time. In Python, you can generate this format as follows:

    from datetime import datetime, timezone now = datetime.now(timezone.utc) formatted_now = now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"

    This code captures the current UTC time, formats it to include milliseconds, and appends 'Z' to denote UTC.

    2. Construct the OData Filter String: Ensure that your filter string uses the correct syntax and that the ExpiresOn column exists in your table. The filter string should look like this:

    my_filter = f"ExpiresOn lt datetime'{formatted_now}'"

    3. Query the Table: Use the constructed filter string to query the table:

    entities = table_client.query_entities(my_filter)

    4. Common Pitfalls to Avoid:

    • Column Name Accuracy: Ensure that the ExpiresOn column exists in your table and that there are no typos.
    • Data Presence: Verify that there are records in the table where ExpiresOn is less than the current time.
    • Time Zone Considerations: Confirm that the ExpiresOn values are stored in UTC. If they're in a different time zone, adjust the formatted_now variable accordingly.

    By following these steps, you should be able to filter your Azure Table Storage entities based on DateTime values effectively.

    Please feel free to click the 'Upvote' (Thumbs-up) button and 'Accept as Answer'. This helps the community by allowing others with similar queries to easily find the solution.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.