SoFunction
Updated on 2025-03-04

N'' and ::bpchar usage in PostgreSQL

PostgreSQL N'' and ::bpchar

The effect of N’' is similar to that of ::bpchar

Both represent fixed-length strings.

For example, the following sql:

select n'233' as num;
select '233'::bpchar as num;
select '233' as num;

The above SQL will get the result set of '233'.

However, the strings obtained by the first two SQL are bpchar, which is equivalent to the char type of MySQL; while the string obtained by the third SQL is text type.

  • VARCHAR(n) varchar specifies the maximum length, variable-length string, and the parts that are insufficient to define the length are not filled.
  • CHAR(n) bpchar is a fixed-length string. When the actual data is insufficient to define the length, it is filled with spaces.
  • TEXT text has no special upper limit (only limited by the maximum length of the row)

For:bpchar

In fact, it means more transformation. Compared to N’’, it can be used like this:

select 233 as num;
select 233::bpchar as num;
  • The first SQL gets the numeric type.
  • The second SQL gets a string, and the value 233 is transformed into a bpchar type

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.