gpt4 book ai didi

sql - MSSQL 子字符串并保持最后一个单词完整

转载 作者:行者123 更新时间:2023-12-03 01:13:41 24 4
gpt4 key购买 nike

我有以下示例字符串:

This string is very large, it has more then 160 characters. We can cut it with substring so it just has 160 characters but then it cuts of the last word that looks kind of stupid.

现在我想要大约 160 个字符,所以我使用 substring()

SELECT SUBSTRING('This string is very large, it has more then 160 characters. 
We can cut it with substring so it just has 160 characters but then it cuts
of the last word that looks kind of stupid.', 0 , 160)

结果是:

This string is very large, it has more then 160 characters. We can cut it with substring so it just has 160 characters but then it cuts of the last word that l

现在我需要找到一种方法来完成最后一个单词,在本例中是单词looks

知道解决这个问题的最佳方法是什么吗?

最佳答案

DECLARE @S VARCHAR(500)= 'This string is very large, it has more then 160 characters. 
We can cut it with substring so it just has 160 characters but then it cuts
of the last word that looks kind of stupid.'

SELECT CASE
WHEN charindex(' ', @S, 160) > 0 THEN SUBSTRING(@S, 0, charindex(' ', @S, 160))
ELSE @S
END

关于sql - MSSQL 子字符串并保持最后一个单词完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18499409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com