Search code examples
sqldatatypespostgresql-12

postgresql 12 function strange value / length behaviour


I have a char(13) column named a in table test.

I created a classic before insert function with the following line :

raise notice 'a: -->%<-- len = %', new.a, length(new.a);

When I run insert into test (a) values('1');, I get the following output :

a: -->1            <-- len = 1

Strange, no ?


Solution

  • No, not strange at all. char is a blank padded data type. So if you assign '1' to your column, it is actually stored as '1 '.

    The solution is simple: don't use char(n)