{"id":36334,"date":"2024-06-22T11:13:24","date_gmt":"2024-06-22T01:13:24","guid":{"rendered":"https:\/\/database.guide\/?p=36334"},"modified":"2024-06-22T11:13:26","modified_gmt":"2024-06-22T01:13:26","slug":"how-the-substring-function-works-in-postgresql","status":"publish","type":"post","link":"https:\/\/database.guide\/how-the-substring-function-works-in-postgresql\/","title":{"rendered":"How the SUBSTRING() Function Works in PostgreSQL"},"content":{"rendered":"\n<p class=\"\">In PostgreSQL, we can use the <code>substring()<\/code> function to extract the first substring matching the specified POSIX or SQL regular expression.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">POSIX Regular Expressions<\/h2>\n\n\n\n<p class=\"\">When used with POSIX regular expressions, the syntax of <code>substring()<\/code> goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>substring ( string text FROM pattern text )<\/code><\/pre>\n\n\n\n<p class=\"\">Here&#8217;s an example that uses a POSIX regular expression:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT substring('Restaurant' FROM 'R..t');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Rest<\/pre>\n\n\n\n<p class=\"\">In POSIX, the dot (<code>.<\/code>) matches any character. We had two dots which matched <code>e<\/code> and <code>s<\/code>, and so the function returned the substring <code>Rest<\/code> because that&#8217;s the first instance in our string that matches this particular POSIX regular expression.<\/p>\n\n\n\n<p class=\"\">Here are some more POSIX examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\x\nSELECT \n    substring('Restaurant' FROM 'Rest') AS \"Rest\",\n    substring('Restaurant' FROM 'R.t') AS \"R.t\",\n    substring('Restaurant' FROM 'R*t') AS \"R*t\",\n    substring('Restaurant' FROM 'r..t') AS \"r..t\",\n    substring('Restaurant' FROM '^R') AS \"^R\",\n    substring('Restaurant' FROM '^r') AS \"^r\",\n    substring('Restaurant' FROM 'beer|sta') AS \"beer|sta\";<\/code><\/pre>\n\n\n\n<p class=\"\">Result (using vertical output):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Rest     | Rest<br>R.t      | null<br>R*t      | t<br>r..t     | rant<br>^R       | R<br>^r       | null<br>beer|sta | sta<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">SQL Regular Expressions<\/h2>\n\n\n\n<p class=\"\">When used with SQL regular expressions, the syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>substring ( string text SIMILAR pattern text ESCAPE escape text ) <\/code><\/pre>\n\n\n\n<p class=\"\">The above form was specified in SQL:2003. <\/p>\n\n\n\n<p class=\"\">The following form was specified in SQL:1999 and is considered obsolete:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>substring ( string text FROM pattern text FOR escape text )<\/code><\/pre>\n\n\n\n<p class=\"\">So we should focus only on the first syntax.<\/p>\n\n\n\n<p class=\"\">Here&#8217;s an example that uses a SQL regular expression:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT substring('Restaurant' SIMILAR '%#\"R__t#\"%' ESCAPE '#');<\/code><\/pre>\n\n\n\n<p class=\"\">Result (using vertical output):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Rest<\/pre>\n\n\n\n<p class=\"\">To indicate the part of the pattern for which the matching data sub-string is of interest, the pattern should contain two occurrences of the escape character followed by a double quote (<code>\"<\/code>). The text matching the portion of the pattern between these separators is returned when the match is successful.<\/p>\n\n\n\n<p class=\"\">Here are some more examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    substring('Restaurant' SIMILAR 'R%t' ESCAPE '#') AS \"R%t\",\n    substring('Restaurant' SIMILAR 's%a' ESCAPE '#') AS \"s%a\",\n    substring('Restaurant' SIMILAR '%#\"s_a#\"%' ESCAPE '#') AS \"%#\"\"s_a#\"\"%\",\n    substring('Restaurant' SIMILAR '%#\"s%a#\"%' ESCAPE '#') AS \"%#\"\"s%a#\"\"%\",\n    substring('Restaurant' SIMILAR '%(sta|xyz)%' ESCAPE '#') AS \"%(sta|xyz)%\",\n    substring('Restaurant' SIMILAR '%#\"(sta|xyz)#\"%' ESCAPE '#') AS \"%#\"\"(sta|xyz)#\"\"%\";<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">R%t             | Restaurant<br>s%a             | null<br>%#\"s_a#\"%       | sta<br>%#\"s%a#\"%       | staura<br>%(sta|xyz)%     | Restaurant<br>%#\"(sta|xyz)#\"% | sta<\/pre>\n\n\n\n<p class=\"\"><\/p>\n\n\n\n<p class=\"\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PostgreSQL, we can use the substring() function to extract the first substring matching the specified POSIX or SQL regular expression.<\/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-36334","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\/36334","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=36334"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36334\/revisions"}],"predecessor-version":[{"id":36535,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36334\/revisions\/36535"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=36334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=36334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=36334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}