{"id":1027,"date":"2021-10-06T00:23:58","date_gmt":"2021-10-05T18:53:58","guid":{"rendered":"https:\/\/techieshouts.com\/home\/?p=1027"},"modified":"2022-08-09T19:02:43","modified_gmt":"2022-08-09T13:32:43","slug":"instr-function-in-teradata","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/instr-function-in-teradata\/","title":{"rendered":"INSTR function in Teradata"},"content":{"rendered":"\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Teradata\" target=\"_blank\" rel=\"noopener\">Teradata<\/a> has multiple functions to work with strings. INSTR function is a very handy one when it comes to search a string inside another. In the other blog, we saw how this is done using the INDEX function. In this, we will see the usage of the INSTR function.<\/p>\n\n\n\n<p>Unlike the INDEX function, the INSTR function has much more ability to specify the position from where the search can be started. Another feature of this function is that it also gives us the flexibility to find the nth occurrence of a string.<\/p>\n\n\n\n<h2>Syntax<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">INSTR(String expression, String to search, Starting position, Nth occurrence)<\/pre>\n\n\n\n<p><strong>String expression<\/strong> &#8211; The full string value inside which the search will be done<\/p>\n\n\n\n<p><strong>String to search<\/strong> &#8211; The string to search inside the original string value<\/p>\n\n\n\n<p><strong>Starting position<\/strong> &#8211; The position from where the search can be done<\/p>\n\n\n\n<p><strong>Nth occurrence<\/strong> &#8211; The nth occurrence of the searched string<\/p>\n\n\n\n<p>Let us see an example to understand all possible usage of the INSTR function.<\/p>\n\n\n\n<p>Imagine you are searching for the keyword &#8216;ERROR&#8217; inside a column where the application&#8217;s error message is stored. There are multiple ways in which the INSTR function can be used in this scenario.<\/p>\n\n\n\n<h2>Case 1 &#8211; With two arguments<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT INSTR('ERROR: Your query failed with SYNTAX ERROR ','ERROR') pos;<\/pre>\n\n\n\n<p>In the above example, we are only using two arguments. The result will give the position of the word &#8216;ERROR&#8217;. This case is the same as the INDEX function. The result will give the position of the first occurrence of the word &#8216;ERROR&#8217;<\/p>\n\n\n\n<h3>Output &#8211; Case 1<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"349\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-9-1024x349.png\" alt=\"\" class=\"wp-image-1029\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-9-1024x349.png 1024w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-9-300x102.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-9-768x262.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-9-660x225.png 660w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-9.png 1204w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2>Case 2 &#8211; With three arguments<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT INSTR('ERROR: Your query failed with SYNTAX ERROR ','ERROR', 3) pos;<\/pre>\n\n\n\n<p>In the above query,  you can see that we have used three arguments. The third argument will tell the query to start the search from the mentioned position. If you notice, there are two places where the word &#8216;ERROR&#8217; is present in the string. One is at the first position and the other is at the 38th position. Since we have given the starting position of the search as 3, it will ignore the first occurrence of the word &#8216;ERROR&#8217; which is at the first position.<\/p>\n\n\n\n<h3>Output &#8211; Case 2<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"343\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-10-1024x343.png\" alt=\"\" class=\"wp-image-1030\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-10-1024x343.png 1024w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-10-300x100.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-10-768x257.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-10-660x221.png 660w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-10.png 1224w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The same query with position value as 1 will return the output as 1 as we have the word &#8216;ERROR&#8217; present at the first position.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT INSTR('ERROR: Your query failed with SYNTAX ERROR ','ERROR', 1) pos;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"343\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-11-1024x343.png\" alt=\"\" class=\"wp-image-1031\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-11-1024x343.png 1024w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-11-300x100.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-11-768x257.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-11-660x221.png 660w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-11.png 1224w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2>Case 3 &#8211; With four arguments<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT INSTR('ERROR: Your query failed with SYNTAX ERROR ','ERROR', 1, 2) pos;<\/pre>\n\n\n\n<p>In this query, we have included all the possible number of arguments supported by the INSTR function. The fourth argument here represents the Nth occurrence of the word that we are searching for.<\/p>\n\n\n\n<p>The 4th argument is given as 2. This means, that we are searching for the second occurrence of the word &#8216;ERROR&#8217; starting from the first position.<\/p>\n\n\n\n<h3>Output &#8211; Case 3<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"335\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-12-1024x335.png\" alt=\"\" class=\"wp-image-1032\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-12-1024x335.png 1024w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-12-300x98.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-12-768x251.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-12-660x216.png 660w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-12.png 1254w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the same example, if you change the fourth argument value to 1, you will get the output position as 1 as we have the first occurrence of the word &#8216;ERROR&#8217; in the first position itself<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT INSTR('ERROR: Your query failed with SYNTAX ERROR ','ERROR', 1, 1) pos;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"335\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-13-1024x335.png\" alt=\"\" class=\"wp-image-1033\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-13-1024x335.png 1024w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-13-300x98.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-13-768x251.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-13-660x216.png 660w, https:\/\/techieshouts.com\/wp-content\/uploads\/2021\/10\/image-13.png 1254w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Also read,<\/p>\n\n\n\n<ol><li><a href=\"https:\/\/techieshouts.com\/home\/index-function-in-teradata\/\">INDEX function in Teradata<\/a><\/li><li><a href=\"https:\/\/techieshouts.com\/home\/concatenation-of-columns-values-in-teradata\/\">Concatenation operation in Teradata<\/a><\/li><li><a href=\"https:\/\/techieshouts.com\/home\/substring-in-teradata\/\">Substring function in Teradata<\/a><\/li><li><a href=\"https:\/\/techieshouts.com\/home\/teradata-get-current-date-and-time\/\">Fetching Current Date and Time in Teradata<\/a><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Teradata has multiple functions to work with strings. INSTR function is a very handy one when it comes to search a string inside another. In the other blog, we saw how this is done using the INDEX function. In this, we will see the usage of the INSTR function. Unlike the INDEX function, the INSTR\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/instr-function-in-teradata\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[184,183,186,211,235,188,182,187],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/1027"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=1027"}],"version-history":[{"count":3,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/1027\/revisions"}],"predecessor-version":[{"id":1036,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/1027\/revisions\/1036"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}