string - Postgres : limit length of returned string but needs to end on a full word

标签 string postgresql

对于 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/

相关文章:

php - 截断 UTF-8 字符串以适应 PHP 中给定的字节数

sql - 获取 '/' 字符后的字符串

sql - 在 PostgreSQL 上以正确的顺序对数组元素进行分组

c - 错误 : Abort trap 6 - String obfuscation

objective-c - NSArray 的字符串排序问题

sql - 在 postgresql 的单个查询中使用 WITH + DELETE 子句

sql - 不使用循环计算行程次数

python - 在 Post 请求正文中发送列表值以验证 Pydantic 模型

php - 仅删除 2 个字符串之间的空格

python - 在 Python 中反转字符串