1

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

SELECT CHAR(65)

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

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

Evan Carroll
65.7k50 gold badges259 silver badges510 bronze badges
asked Feb 22, 2018 at 17:32
1
  • 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. Commented Feb 23, 2018 at 2:36

1 Answer 1

2

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)
 ?column? 
----------
 A
(1 row)
Evan Carroll
65.7k50 gold badges259 silver badges510 bronze badges
answered Feb 22, 2018 at 17:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.