{"id":2339,"date":"2021-01-21T08:00:11","date_gmt":"2021-01-21T16:00:11","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=2339"},"modified":"2021-01-20T13:40:17","modified_gmt":"2021-01-20T21:40:17","slug":"like-keyword-cosmosdb","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/like-keyword-cosmosdb\/","title":{"rendered":"Introducing the LIKE keyword in Azure Cosmos DB"},"content":{"rendered":"<p>You can now use the LIKE keyword to do text searches in Azure Cosmos DB SQL (core) API! The LIKE keyword has been <a href=\"https:\/\/feedback.azure.com\/forums\/263030-azure-cosmos-db\/suggestions\/6333414-implement-like-keyword\">a top Azure Cosmos DB feature request<\/a> and many use cases will see tremendous value from new string search options in Azure Cosmos DB.<\/p>\n<p>By including the LIKE keyword in a WHERE clause, you can search for specific string patterns. LIKE is a great addition to the existing string search system functions in Azure Cosmos DB:<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"312\"><strong>System function<\/strong><\/td>\n<td width=\"312\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"312\">RegexMatch<\/td>\n<td width=\"312\">Checks if a string contains a specific regular expression<\/td>\n<\/tr>\n<tr>\n<td width=\"312\">Contains<\/td>\n<td width=\"312\">Checks if a string contains a specific string value<\/td>\n<\/tr>\n<tr>\n<td width=\"312\">StringEquals<\/td>\n<td width=\"312\">Checks if a string equals a specific string value<\/td>\n<\/tr>\n<tr>\n<td width=\"312\">StartsWith<\/td>\n<td width=\"312\">Checks if a string starts with a specific string value<\/td>\n<\/tr>\n<tr>\n<td width=\"312\">EndsWith<\/td>\n<td width=\"312\">Checks if a string ends with a specific string value<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>In this blog, we will run some sample queries using the LIKE keyword. All sample queries in this doc can be run against a dataset that is preloaded on the <a href=\"https:\/\/www.documentdb.com\/sql\/demo\">Azure Cosmos DB Query Playground<\/a>.<\/p>\n<p>Using LIKE, you can check if a string contains a specific pattern, including several different wildcard characters:<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"208\">Wildcard character<\/td>\n<td width=\"208\">Description<\/td>\n<\/tr>\n<tr>\n<td width=\"208\">%<\/td>\n<td width=\"208\">Any string of zero or more characters<\/td>\n<\/tr>\n<tr>\n<td width=\"208\">_ (underscore)<\/td>\n<td width=\"208\">Any single character<\/td>\n<\/tr>\n<tr>\n<td width=\"208\">[ ]<\/td>\n<td width=\"208\">Any single character within the specified range ([a-f]) or set ([abcdef]).<\/td>\n<\/tr>\n<tr>\n<td width=\"208\">[^]<\/td>\n<td width=\"208\">Any single character not within the specified range ([^a-f]) or set ([^abcdef]).<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3><span style=\"font-size: 18pt; font-family: arial, helvetica, sans-serif;\">Examples:<\/span><\/h3>\n<p>Here\u2019s a simple example that uses LIKE with the % wildcard character to return all items with a description that contains \u201ccereal\u201d.<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT\u00a0*\r\nFROM c\r\nWHERE c.description LIKE \"%cereal%\"\r\n\r\n<\/pre>\n<p>Here\u2019s another example that uses the _ (underscore) to match a single character. This query will return all items with a description with a list that contains a section that starts with \u201cBaby\u201d and has any four letters and a comma afterwards.<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT\u00a0*\r\nFROM c\r\nWHERE c.description LIKE \"Baby____,%\"<\/pre>\n<p>&nbsp;<\/p>\n<p>This example uses the [ ] wildcard to check if the description contains \u201cFruit pudding\u201d or \u201cfruit pudding\u201d.<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT\u00a0*\r\nFROM c\r\nWHERE c.description LIKE \"%[Ff]ruit pudding%\"\r\n\r\n<\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif; font-size: 18pt;\">Using the ESCAPE clause:<\/span><\/p>\n<p>You can search for patterns that include one or more wildcard characters using the ESCAPE clause. For example, if you wanted to search for descriptions that contained the string\u00a0<code>20-30%<\/code>, you wouldn\u2019t want to interpret the\u00a0<code>%<\/code>\u00a0as a wildcard character.<\/p>\n<div id=\"code-try-12\" class=\"codeHeader\" data-bi-name=\"code-header\"><strong>Query:<\/strong><\/div>\n<div data-bi-name=\"code-header\"><\/div>\n<pre>SELECT *\r\nFROM c\r\nWHERE c.description LIKE '%20-30!%%' ESCAPE '!'<\/pre>\n<p>You can write logically equivalent queries uses either the LIKE keyword or the RegexMatch system function. You\u2019ll observe the same query performance regardless of which one you choose, so choose based on preference.<\/p>\n<p><strong>Learn more:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-keywords\">LIKE documentation<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-regexmatch\">RegexMatch documentation\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-system-functions\">String system function documentation<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can now use the LIKE keyword to do text searches in Azure Cosmos DB SQL (core) API! By including the LIKE keyword in a WHERE clause, you can search for specific string patterns.<\/p>\n","protected":false},"author":12128,"featured_media":61,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14,818],"tags":[],"class_list":["post-2339","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api","category-query"],"acf":[],"blog_post_summary":"<p>You can now use the LIKE keyword to do text searches in Azure Cosmos DB SQL (core) API! By including the LIKE keyword in a WHERE clause, you can search for specific string patterns.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/2339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/12128"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=2339"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/2339\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/61"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=2339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=2339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=2339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}