2014-12-01 112 views
3

按照documentation,琴弦比由字符改变或VARCHAR指定的时间更长应被截断:PostgreSQL里没有截断超长字符串

If one explicitly casts a value to character varying(n) or character(n), then an over-length value will be truncated to n characters without raising an error. (This too is required by the SQL standard.)

,但我无法得到它的工作。现在文档确实表示,必须“明确”为字符变化赋予一个值,所以也许我错过了这一点。下面是一个简单的测试表:太长值类型字符改变(20)

insert into test1 values 
('this is a super long string that we want to see if it is really truncated'); 

我怎样才能得到这个工作:

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

在以下失败,错误?

+4

什么文件说长字符串会自动截断?你能引用相关文件或提供链接吗? MySQL做这样的事情(取决于它如何配置),PostgreSQL更明智。 – 2014-12-01 23:58:57

回答

5

这不会截断,因为它只是一个任务:

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

insert into test1 values ('this is a super long string that we want to see if it is really truncated'); 

但这种意愿,因为它是一个显式类型转换

insert into test1 values (CAST('this is a super long string that we want to see if it is really truncated' AS varchar(20))); 

要获得截断行为,则必须使用明确的转换,坦率地说,我希望SQL标准没有具体说明。

更好的方式来处理这个问题是要明确你想要什么:

insert into test1 values (left('this is a super long string that we want to see if it is really truncated', 20)); 
+0

谢谢,这有助于我在插入之前不得不截断 – WraithWireless 2014-12-02 00:41:46