微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sql – 从Postgres中选择截断的字符串

我在Postgres中有一些大的varchar值,我想要SELECT并移动到其他地方.他们要去的地方使用VARCHAR(4095)所以我只需要最多4095个字节(我认为是字节),其中一些变量非常大,所以性能优化就是选择它们的截断版本.

我怎样才能做到这一点?
就像是:

SELECT TruncATED(my_val,4095) ...

我不认为这是一个字符长度,它需要一个字节长度?

解决方法

varchar(n)中的n是字符数(不是字节数). The documentation:

sql defines two primary character types: character varying(n) and
character(n),where n is a positive integer. Both of these types can
store strings up to n characters (not bytes) in length.

大胆强调我的.

“截断”字符串的最简单方法是使用left()

SELECT left(my_val,4095)

或者你可以只是cast

SELECT my_val::varchar(4095)

The manual once more:

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.)

原文地址:https://www.jb51.cc/mssql/75734.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐