Convert non-ascii characters to unicode escape sequence

291 Views Asked by At

Is it any function in postgres sql to convert non ascii characters to unicode e.g. polish letter ż to \u017C?

I've tried to use encode and convert_from build postgres functions but without any results.

1

There are 1 best solutions below

2
RU-D On

Just use ASCII() coupled with to_hex(). Finally the concat() will get you your required unicode.

postgres=# SELECT ascii('ż');
 ascii 
-------
   380
(1 row)

postgres=# SELECT CHR(380);
 chr 
-----
 ż
(1 row)

postgres=# SELECT to_hex(ascii('ż'));
 to_hex 
--------
 17c
(1 row)

postgres=# SELECT concat('\u0', to_hex(ascii('ż')));
 concat 
--------
 \u017c
(1 row)

For additional characters, you may have to improvise using substring()

SELECT concat('\u0', to_hex(ascii('życie')), substring('życie' from 2 for 4));
   concat   
------------
 \u017cycie
(1 row)