{"id":21461,"date":"2021-11-05T07:43:24","date_gmt":"2021-11-04T21:43:24","guid":{"rendered":"https:\/\/database.guide\/?p=21461"},"modified":"2021-11-05T07:43:24","modified_gmt":"2021-11-04T21:43:24","slug":"how-position-works-in-postgresql","status":"publish","type":"post","link":"https:\/\/database.guide\/how-position-works-in-postgresql\/","title":{"rendered":"How POSITION() Works in PostgreSQL"},"content":{"rendered":"\n<p>PostgreSQL has a <code>POSITION()<\/code> function that returns the&nbsp;first starting index of a specified substring within a string.<\/p>\n\n\n\n<p>If the substring doesn&#8217;t exist in the string, then zero is returned.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>position ( substring text IN string text )<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Examples<\/h2>\n\n\n\n<p>Here&#8217;s an example to demonstrate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT POSITION('and' IN 'Two Hands');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">6<\/pre>\n\n\n\n<p>As mentioned, if the substring isn&#8217;t found in the string, zero is returned:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT POSITION('squid' IN 'Two Hands');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Null Arguments<\/h2>\n\n\n\n<p>Null values return <code>null<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\pset null '&lt;null&gt;'\nSELECT \n    POSITION(null IN 'Two Hands') AS \"1\",\n    POSITION('and' IN null) AS \"2\";<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   1    |   2    \n--------+--------\n &lt;null&gt; | &lt;null&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Omitting the Argument<\/h2>\n\n\n\n<p>Omitting the argument results in an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT POSITION();<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR:  function pg_catalog.position() does not exist\nLINE 1: SELECT POSITION();\n               ^\nHINT:  No function matches the given name and argument types. You might need to add explicit type casts.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL has a POSITION() function that returns the&nbsp;first starting index of a specified substring within a string. If the substring doesn&#8217;t exist in the string, then zero is returned.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[93,67,20],"class_list":["post-21461","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\/21461","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=21461"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21461\/revisions"}],"predecessor-version":[{"id":21488,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21461\/revisions\/21488"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=21461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=21461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=21461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}