PostgreSql。连接的奇怪行为

PostgreSql。连接的奇怪行为,postgresql,char,concatenation,Postgresql,Char,Concatenation,很短的问题。有人能说出为什么这个问题吗 select LENGTH(' '::char || ' '::char), LENGTH(' '::text || ' '::char), LENGTH(' ' || ' '), LENGTH('a'::char || 'b'::char); 返回 0 1 2 2 空格是不与其他字符串连接的特殊字符吗 文件仅说明了这一点: Unless otherwise noted, all of the functions listed bel

很短的问题。有人能说出为什么这个问题吗

select LENGTH(' '::char || ' '::char), LENGTH(' '::text || ' '::char), LENGTH(' ' || ' '), LENGTH('a'::char || 'b'::char);
返回

0    1    2    2
空格是不与其他字符串连接的特殊字符吗

文件仅说明了这一点:

Unless otherwise noted, all of the functions listed below work
on all of these types, but be wary of potential effects of
automatic space-padding when using the character type.

我为什么这么做?因为我正在存储过程中逐个字符地构建字符串,当我尝试将varchar与char连接起来时,什么都没有发生。

是“固定长度,空白填充”。这意味着如果将
“foobar”
存储到
char(10)
字段中,Postgres实际上存储
“foobar”
(这是四个尾随空格,因此不保留相邻的空白)。当你取回你的值时,任何尾随的空格都会被去掉。同样的情况也发生在
“”::char
-它的尾部空白被去除,只留下一个零长度的字符串。

因此我将包含current char的变量类型从char(1)更改为varchar(1),所有符号都变好了。谢谢