{"id":21420,"date":"2021-11-08T08:00:45","date_gmt":"2021-11-07T22:00:45","guid":{"rendered":"https:\/\/database.guide\/?p=21420"},"modified":"2021-11-08T08:00:45","modified_gmt":"2021-11-07T22:00:45","slug":"instr-equivalent-in-sql-server","status":"publish","type":"post","link":"https:\/\/database.guide\/instr-equivalent-in-sql-server\/","title":{"rendered":"INSTR() Equivalent in SQL Server"},"content":{"rendered":"\n<p>Many <a href=\"https:\/\/database.guide\/what-is-an-rdbms\/\" data-type=\"post\" data-id=\"222\">RDBMS<\/a>s have an <code>INSTR()<\/code> function that enables us to find a substring within a string. Some (such as MySQL and MariaDB) also have a <code>LOCATE()<\/code> function and a <code>POSITION()<\/code> function (also supported by PostgreSQL), that do a similar thing.<\/p>\n\n\n\n<p>SQL Server doesn&#8217;t have an <code>INSTR()<\/code> function. Nor does it have a <code>LOCATE()<\/code> or <code>POSITION()<\/code> function. But it does have the <code>CHARINDEX()<\/code> function that does the same thing.<\/p>\n\n\n\n<p>SQL Server also has the&nbsp;<code>PATINDEX()<\/code>&nbsp;function, which does a similar job to&nbsp;<code>CHARINDEX()<\/code>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">The <code>CHARINDEX()<\/code> Function<\/h2>\n\n\n\n<p>Here&#8217;s an example of the <code><a href=\"https:\/\/database.guide\/how-the-charindex-function-works-in-sql-server-t-sql\/\" data-type=\"post\" data-id=\"3947\">CHARINDEX()<\/a><\/code> function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT CHARINDEX('news', 'No news is good news');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">4<\/pre>\n\n\n\n<p>The function accepts a third argument that allows us to specify where to start the search:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT CHARINDEX('news', 'No news is good news', 5);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">17<\/pre>\n\n\n\n<p>In this example, we started the search at position 5, which was after the start of the first occurrence of <code>news<\/code>, so it returned the position of the second occurrence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>PATINDEX()<\/code> Function<\/h2>\n\n\n\n<p>The <code><a href=\"https:\/\/database.guide\/how-the-patindex-function-works-in-sql-server-t-sql\/\" data-type=\"post\" data-id=\"4008\">PATINDEX()<\/a><\/code> function is similar to <code>CHARINDEX()<\/code>, except that it allows us to search for a pattern, rather than a specific string.<\/p>\n\n\n\n<p>Here&#8217;s an example to illustrate what I mean:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT PATINDEX('%ew%', 'No news is good news');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5<\/pre>\n\n\n\n<p>Here&#8217;s what happens if I remove the wildcard characters:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT PATINDEX('ew', 'No news is good news');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0<\/pre>\n\n\n\n<p>See <a href=\"https:\/\/database.guide\/charindex-vs-patindex-in-sql-server-whats-the-difference\/\" data-type=\"post\" data-id=\"1739\"><code>PATINDEX()<\/code>vs <code>CHARINDEX()<\/code><\/a> for more on the differences between these two functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many RDBMSs have an INSTR() function that enables us to find a substring within a string. Some (such as MySQL and MariaDB) also have a LOCATE() function and a POSITION() function (also supported by PostgreSQL), that do a similar thing. SQL Server doesn&#8217;t have an INSTR() function. Nor does it have a LOCATE() or POSITION() &#8230; <a title=\"INSTR() Equivalent in SQL Server\" class=\"read-more\" href=\"https:\/\/database.guide\/instr-equivalent-in-sql-server\/\" aria-label=\"Read more about INSTR() Equivalent in SQL Server\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[10,67,20],"class_list":["post-21420","post","type-post","status-publish","format-standard","hentry","category-sql-server","tag-how-to","tag-string-functions","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=21420"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21420\/revisions"}],"predecessor-version":[{"id":21628,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21420\/revisions\/21628"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=21420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=21420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=21420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}