如何使用 SQL Server 截断字符串 [英] How to truncate string using SQL server

查看:43
本文介绍了如何使用 SQL Server 截断字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have large string in SQL Server. I want to truncate that string to 10 or 15 character

原始字符串

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

所需的字符串

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

推荐答案

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

If you only want to return a few characters of your long string, you can use:

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

参见SQL Fiddle with Demo.

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

This will return the first 15 characters of the string and then concatenates the ... to the end of it.

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

If you want to to make sure than strings less than 15 do not get the ... then you can use:

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

参见SQL Fiddle with Demo

这篇关于如何使用 SQL Server 截断字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆