对于 Postgres,我试图在返回字符串之前截断它,以便我可以在客户端的部分结果列表中显示它。关键是它需要以一个完整的词结尾。我已经做到了:
SELECT comments, substr(comments, 1, 80) AS "trunc"FROM book;
下一步是否是使用 RegEx 对子字符串进行子字符串处理(可能试图确保字符串以 a 结尾)空间)?
最佳答案
使用substring(string from pattern)
. 12 个字符的示例:
with the_data(comments) as (
values
('follow me'::text),
('abcdef ghijkl'),
('abcd efgh ijkl'),
('abc def ghi jkl'),
('ab cd ef gh ij kl')
)
select substring(left(comments || ' ', 12) from '.*\s')
from the_data;
substring
--------------
follow me
abcdef
abcd efgh
abc def ghi
ab cd ef gh
(5 rows)
关于string - Postgres : limit length of returned string but needs to end on a full word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38383853/