gpt4 book ai didi

sql-server - 如何使用 SQL Server 截断字符串

转载 作者:行者123 更新时间:2023-12-01 16:55:09 26 4
gpt4 key购买 nike

我在 SQL Server 中有一个大字符串。我想将该字符串截断为 10 或 15 个字符

原始字符串

this is test string. this is test string. this is test string. this is test string.

所需的字符串

this is test string. this is ......

最佳答案

如果您只想返回长字符串中的几个字符,您可以使用:

select 
left(col, 15) + '...' col
from yourtable

参见SQL Fiddle with Demo .

这将返回字符串的前 15 个字符,然后将 ... 连接到其末尾。

如果您想确保小于 15 的字符串不会得到 ... 那么您可以使用:

select 
case
when len(col)>15
then left(col, 15) + '...'
else col end col
from yourtable

参见SQL Fiddle with Demo

关于sql-server - 如何使用 SQL Server 截断字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15142356/

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