{"id":36714,"date":"2024-07-27T09:58:42","date_gmt":"2024-07-26T23:58:42","guid":{"rendered":"https:\/\/database.guide\/?p=36714"},"modified":"2024-07-27T09:58:44","modified_gmt":"2024-07-26T23:58:44","slug":"understanding-postgresqls-unistr-function","status":"publish","type":"post","link":"https:\/\/database.guide\/understanding-postgresqls-unistr-function\/","title":{"rendered":"Understanding PostgreSQL&#8217;s UNISTR() Function"},"content":{"rendered":"\n<p class=\"\">PostgreSQL has a <code>unistr()<\/code> function that evaluates escaped Unicode characters in its argument and returns the result as text.<\/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 unistr('\\0063\\0061\\0066\\00E9');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">caf\u00e9<\/pre>\n\n\n\n<p class=\"\">Unicode characters can be specified as <code>\\XXXX<\/code> (4 hexadecimal digits), <code>\\+XXXXXX<\/code> (6 hexadecimal digits), <code>\\uXXXX<\/code> (4 hexadecimal digits), or <code>\\UXXXXXXXX<\/code> (8 hexadecimal digits).<\/p>\n\n\n\n<p class=\"\">Therefore, we can do any of the following to get the same result as the above example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    unistr('\\0063\\0061\\0066\\00E9') AS \"\\XXXX\",\n    unistr('\\+000063\\+000061\\+000066\\+0000E9') AS \"+XXXXXX\",\n    unistr('\\u0063\\u0061\\u0066\\u00E9') AS \"\\uXXXX\",\n    unistr('\\U00000063\\U00000061\\U00000066\\U000000E9') AS \"\\UXXXXXXXX\";<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> \\XXXX | +XXXXXX | \\uXXXX | \\UXXXXXXXX <br>-------+---------+--------+------------<br> caf\u00e9  | caf\u00e9    | caf\u00e9   | caf\u00e9<\/pre>\n\n\n\n<p class=\"\">If we want to include a backslash, we can escape it with another backslash. In other words, to include a backslash use two backslashes.<\/p>\n\n\n\n<p class=\"\">If the server encoding is not UTF-8, the Unicode code point identified by one of these escape sequences is converted to the actual server encoding. If it&#8217;s not possible to do that, then an error occurs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Null Argument<\/h2>\n\n\n\n<p class=\"\">If the argument is <code>null<\/code> then <code>null<\/code> is returned:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT unistr(null);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">null<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Wrong Argument Type<\/h2>\n\n\n\n<p class=\"\">Passing an argument of the wrong type results in an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT unistr(123);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR:  function unistr(integer) does not exist<br>LINE 1: SELECT unistr(123);<br>               ^<br>HINT:  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 unistr() function that evaluates escaped Unicode characters in its argument and returns the result as text.<\/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-36714","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\/36714","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=36714"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36714\/revisions"}],"predecessor-version":[{"id":36721,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36714\/revisions\/36721"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=36714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=36714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=36714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}