SQL Server has CHAR ( integer_expression ), so I can do


What is the equivalent on Postgresql? I have tried SELECT E'\0x65'; but I get

ERROR: invalid byte sequence for encoding "UTF8": 0x00

  • I think there are two problems, here 0x92 is 146. That's >128 which means it's not ASCII and is instead encoding-dependent. That doesn't seem to be what you want. Perhaps you just mean 92 (which is different from 0x92). Or, perhaps you do mean 0x92 which is valid in Win1252 but would require reencoding. Because that's a fundamentally different problem I'm going to alter your question to use 65 which is ASCII for A. – Evan Carroll Feb 23 '18 at 2:36

Have a look at the docs on PostgreSQL String Functions and Operators,

chr(int) Character with the given code. For UTF8 the argument is treated as a Unicode code point. For other multibyte encodings the argument must designate an ASCII character. The NULL (0) character is not allowed because text data types cannot store such bytes.

Demonstrating chr(), its inverse, and both.,

# SELECT chr(65), ascii('A'), chr(ascii('A'));
 chr | ascii | chr 
 A   |    65 | A
(1 row)

In your question you use E'' (the "bytea Hex Format") which is totally possible too, though I wouldn't suggest it for simple problems like this. You can do that like this,

SELECT E'\x41'; -- 41 is hex for 65 (in base 10)
SELECT E'\101'; -- 101 is octal for 65 (in base 10)
(1 row)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.