sql insert - Postgresql not truncating overlength strings -


according documentation, strings longer specified character varying or varchar should truncated:

if 1 explicitly casts value character varying(n) or character(n), over-length value truncated n characters without raising error. (this required sql standard.)

but can not work. documentation 1 has "explicitly" cast value character varying maybe missing that. below simple test table:

create table test1( tval character varying(20)); 

where following fails error: value long type character varying(20)

insert test1 values  ('this super long string want see if truncated'); 

how can work?

this won't truncate, because it's assignment:

create table test1(tval character varying(20));  insert test1 values ('this super long string want see if truncated'); 

but will, because it's explicit cast:

insert test1 values (cast('this super long string want see if truncated' varchar(20))); 

to truncation behaviour must use explicit cast, , frankly wish sql standard didn't specify that.

the better way handle explicit want:

insert test1 values (left('this super long string want see if truncated', 20)); 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -