ADF Dynamic SQL as query in cosmosdb returning syntax error

Indira Priya Darshini Bandari 0 Reputation points MVP
2024-08-07T02:40:27.97+00:00

I am trying to get the previous days records from cosmosdb using the below query

I have defined a variable named start using the set varaible.

@concat('select * from c where ', formatDateTime('[c.createdAt]','yyyy-MM-dd'), '>=', '''', variables('start'), '''')

I get a syntax error.

Can anyone please help me with what is wrong with the query ?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
{count} votes

1 answer

Sort by: Most helpful
  1. Vinodh247 40,041 Reputation points MVP Volunteer Moderator
    2024-08-07T05:04:01.94+00:00

    Hi [Indira Priya Darshini Bandari],

    Thanks for reaching out to Microsoft Q&A.

    1. It seems the way you're trying to format and concatenate the date variable into the SQL query is incorrect. The formatDateTime function should be used to format the variable before concatenation.
    2. When you are constructing sql queries, ensure that string literals are properly enclosed in single quotes. You need to escape single quotes correctly in adf expressions.

    Refer query #1

    In SQL, string literals must be enclosed in single quotes. To include a single quote in a string in ADF, you need to use two single quotes (' ') . Ensure that variable start is correctly set to a valid date string before using it in the query.

    For ex, If your variable start is meant to represent the previous day's date...

    Refer query #2

    User's image

    can you try this and let me know

    Please 'Upvote'(Thumbs-up) and 'Accept' as an answer if the reply was helpful. This will benefit other community members who face the same issue.

    0 comments No comments

Your answer

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