Steven C
Steven C

Reputation: 135

How to use "LIKE" in parameterized Cosmos query

I'm using The Azure.Cosmos API, version 3.17.1. If I run a query against my Cosmos collection as so, I get the results I expect:

string Sql = "SELECT c.LastName FROM c where c.LastName like 'Smi%'";
QueryDefinition oQry = new QueryDefinition(Sql);
FeedIterator<myObj> oFI = this.container.GetItemQueryIterator<myObj>(oQry); // returns Smith, Smithers, etc.

If I try to parameterize this, I get nothing back:

string Sql = "SELECT c.LastName FROM c where c.LastName like '@KeyWord%'";
QueryDefinition oQry = new QueryDefinition(Sql);
oQry.WithParameter(@KeyWord, "Smi");
FeedIterator<myObj> oFI = this.container.GetItemQueryIterator<myObj>(oQry);

Is this a syntax issue or something not supported?

Tks

Upvotes: 2

Views: 1722

Answers (1)

Steve Johnson
Steve Johnson

Reputation: 8660

Don't use % in your parameter names and don't need to quote it.

Please try this code:

string Sql = "SELECT c.LastName FROM c where c.LastName like @KeyWord";
QueryDefinition oQry = new QueryDefinition(Sql);
oQry.WithParameter("@KeyWord", "Smi%");
FeedIterator<myObj> oFI =  container.GetItemQueryIterator<myObj>(oQry);

Upvotes: 5

Related Questions