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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...