Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Postgres:限制返回字符串的长度,但需要以完整单词结尾_String_Postgresql - Fatal编程技术网

String Postgres:限制返回字符串的长度,但需要以完整单词结尾

String Postgres:限制返回字符串的长度,但需要以完整单词结尾,string,postgresql,String,Postgresql,对于Postgres,我试图在返回字符串之前截断它,以便在客户端的部分结果列表中显示它。关键是它需要以一个完整的单词结尾。我已经做到了: 从书中选择注释,substr(注释,1,80)作为“trunc”下一步是使用RegEx执行子字符串的子字符串吗(可能是尝试确保字符串以空格结尾) 使用。12个字符的示例: with the_data(comments) as ( values ('follow me'::text), ('abcdef ghijkl'),

对于Postgres,我试图在返回字符串之前截断它,以便在客户端的部分结果列表中显示它。关键是它需要以一个完整的单词结尾。我已经做到了:
从书中选择注释,substr(注释,1,80)作为“trunc”
下一步是使用RegEx执行子字符串的子字符串吗(可能是尝试确保字符串以空格结尾)

使用。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)

如果您添加此示例
('follow me'::text)
-它会切断'me',但我对OPs问题的解释是,它应该返回'follow me'(在本例中)。如果您只是在
注释中的值后面添加一个空格,似乎可以解决此问题:
选择子字符串(左(注释| |',12)自'.*\s')从_数据