我在SQL Server中有大字符串.我想将该字符串截断为10或15个字符
原始字符串
this is test string. this is test string. this is test string. this is test string.
Run Code Online (Sandbox Code Playgroud)
期望的字符串
this is test string. this is ......
Run Code Online (Sandbox Code Playgroud)
Tar*_*ryn 146
如果您只想返回长字符串的几个字符,可以使用:
select
left(col, 15) + '...' col
from yourtable
Run Code Online (Sandbox Code Playgroud)
这将返回字符串的前15个字符,然后将其连接...到结尾.
如果你想确保小于15的字符串没有得到,...那么你可以使用:
select
case
when len(col)>=15
then left(col, 15) + '...'
else col end col
from yourtable
Run Code Online (Sandbox Code Playgroud)
sna*_*ton 28
您可以使用
LEFT(column, length)
Run Code Online (Sandbox Code Playgroud)
要么
SUBSTRING(column, start index, length)
Run Code Online (Sandbox Code Playgroud)
小智 5
您还可以使用 Cast() 操作:
Declare @name varchar(100);
set @name='....';
Select Cast(@name as varchar(10)) as new_name
Run Code Online (Sandbox Code Playgroud)