首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用SQL server截断字符串

如何使用SQL server截断字符串
EN

Stack Overflow用户
提问于 2013-03-01 01:56:21
回答 6查看 259.5K关注 0票数 123

我在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 ......
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-03-01 01:58:06

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

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

票数 184
EN

Stack Overflow用户

发布于 2013-03-01 02:24:05

您可以使用

LEFT(column, length)

SUBSTRING(column, start index, length)
票数 44
EN

Stack Overflow用户

发布于 2015-03-25 17:14:37

您还可以使用Cast()操作:

 Declare @name varchar(100);
set @name='....';
Select Cast(@name as varchar(10)) as new_name
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15142356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档