{"id":36664,"date":"2024-06-27T11:09:13","date_gmt":"2024-06-27T01:09:13","guid":{"rendered":"https:\/\/database.guide\/?p=36664"},"modified":"2024-06-27T11:09:14","modified_gmt":"2024-06-27T01:09:14","slug":"how-strpos-works-in-postgresql","status":"publish","type":"post","link":"https:\/\/database.guide\/how-strpos-works-in-postgresql\/","title":{"rendered":"How STRPOS() Works in PostgreSQL"},"content":{"rendered":"\n<p class=\"\">In PostgreSQL, the <code>strpos()<\/code> function returns the first starting position of a given substring within a string.<\/p>\n\n\n\n<p class=\"\">We pass the string as the first argument and the substring as the second.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p class=\"\">Here&#8217;s an example to demonstrate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT strpos('PostgreSQL', 'SQL');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">8<\/pre>\n\n\n\n<p class=\"\">It&#8217;s the same result we&#8217;d get if we were to use the <a href=\"https:\/\/database.guide\/how-position-works-in-postgresql\/\" data-type=\"post\" data-id=\"21461\"><code>position()<\/code> function<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT position('SQL' in 'PostgreSQL');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">8<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">No Match<\/h2>\n\n\n\n<p class=\"\">If the substring doesn&#8217;t exist in the string, then the function returns zero:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT strpos('PostgreSQL', 'sql');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0<\/pre>\n\n\n\n<p class=\"\">In this case I used the same substring, except this time it was in lowercase. This resulted in <code>0<\/code> being returned due to no match.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Null Arguments<\/h2>\n\n\n\n<p class=\"\">If any of the arguments is <code>null<\/code>, then <code>null<\/code> is returned:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    strpos('PostgreSQL', null) AS \"1\",\n    strpos(null, 'SQL') AS \"2\",\n    strpos(null, null) AS \"3\";<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  1   |  2   |  3   <br>------+------+------<br> null | null | null<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Wrong Argument Type<\/h2>\n\n\n\n<p class=\"\">Both arguments need to be text. Passing the wrong type results in an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT strpos('PostgreSQL', 3);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR:  function strpos(unknown, integer) does not exist<br>LINE 1: SELECT strpos('PostgreSQL', 3);<br>               ^<br>HINT:  No function matches the given name and argument types. You might need to add explicit type casts.<\/pre>\n\n\n\n<p class=\"\">And:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT strpos(123, 'SQL');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR:  function strpos(integer, unknown) does not exist<br>LINE 1: SELECT strpos(123, 'SQL');<br>               ^<br>HINT:  No function matches the given name and argument types. You might need to add explicit type casts.<\/pre>\n\n\n\n<p class=\"\">But of course, we can provide the text representation of such values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT strpos('1234567', '56');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In PostgreSQL, the strpos() function returns the first starting position of a given substring within a string. We pass the string as the first argument and the substring as the second.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[93,67,20],"class_list":["post-36664","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-functions","tag-string-functions","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36664","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=36664"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36664\/revisions"}],"predecessor-version":[{"id":36672,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36664\/revisions\/36672"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=36664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=36664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=36664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}