Very short question. Can anyone say why this query
select LENGTH(' '::char || ' '::char), LENGTH(' '::text || ' '::char), LENGTH(' ' || ' '), LENGTH('a'::char || 'b'::char);
returns
0 1 2 2
Is space a special character witch don’t concatenate with other strings?
Documentation says only this:
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.
Why I do this? Because i’m building string char by char in stored procedure, and when i try to concatenate varchar with char nothing happens.
The CHAR type is “fixed-length, blank padded”. This means that if you store
"foobar"into achar(10)field, Postgres actually stores"foobar "(that’s four trailing spaces, SO does not preserve adjacent whitespace). When you fetch back your value, any trailing whitespace is stripped out. The same happens with' '::char— its trailing whitespace is stripped, leaving only a zero-length string.