How to convert literal \u sequences into UTF-8? [duplicate]

两盒软妹~` 提交于 2019-11-28 11:27:59

问题


This question already has an answer here:

  • Convert escaped Unicode character back to actual character in PostgreSQL 1 answer

I am loading data dump from external source and some strings contain \uXXXX sequences for the UTF8 chars, like this one:

\u017D\u010F\u00E1r nad S\u00E1zavou

I can check the contents by using E'' constant in psql, but cannot find any function/operator to return me proper value.

I'd like to ask, if it's possible to convert this string with unicode escapes into normal UTF8 without using PL/pgSQL functions?


回答1:


I don't think there is a built in method for that. Easiest way I can think of is the plpgsql function you wanted to avoid:

CREATE OR REPLACE FUNCTION str_eval(text, OUT t text) AS
$func$
BEGIN
EXECUTE 'SELECT E''' || replace($1, '''', '''''') || ''''
USING $1
INTO t;
END
$func$ LANGUAGE plpgsql IMMUTABLE STRICT;

The updated version safeguards against SQLi and is faster, too.



来源:https://stackoverflow.com/questions/10111654/how-to-convert-literal-u-sequences-into-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!