如何在SQL中仅显示文本中的x个字符? [英] How show only x number of characters from a text in SQL?
本文介绍了如何在SQL中仅显示文本中的x个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,例如:@ text ='这里,是我的文字'。我想只显示前10个字符而不会中断单词。
我想显示@ limit = 10:@ text ='这里,是'not @ text ='这里,是m'。我怎么能在一个表格的字段中这样做?
谢谢!
解决方案
试试这个substring
inSql
< span class =code-keyword>声明 @ limit int = 10;
声明 @ text nvarchar (max)= ' 这是我的文字';
选择 substring( @ text , 1 , @ limit )
结果:
'这里,是m'
你有很少的函数来获取字符串的一部分:
LEFT [ ^ ]
权利 [ ^ ]
SUBSTRING [ ^ ]
如果你想找一个角色,那就可以了'限制',使用 CHARINDEX [ ^ ]函数togheter具有上述功能之一。请参阅 PATINDEX [ ^ ]
DECLARE @ t = ' < span class =code-string>这是我的文字'
SELECT LEFT ( @ t ,CHARINDEX(' ', @ t )) AS CuttedText
这应该适合你。
声明 @ text Nvarchar ( 500 )= < span class =code-string>'这里是我的文字'
声明 @ limit int = 10
声明 @ CuttText Nvarchar ( 500 )
选择 @ CuttText = Substring( @ text , 1 , @ limit )
声明 @ Spaceindex int ,< span class =code-sdkkeyword> @ Reverse Nvarchar ( 100 )
set @ Reverse = Reverse( @ CuttText )
选择 @ Spaceindex = Charindex(' ', @ Reverse )
选择反向(子串( @ Reverse ,@ SpaceIndex + 1,LEN( @ CuttText )))
I have a string, for example: @text='Here, is my text'. I want to show only first 10 characters without break the words.
I want to show for @limit=10: @text='Here, is' not @text='Here, is m'. How can i do this in a field from a table for example?
Thank you!
解决方案
Try thissubstring
inSql
declare @limit int=10; declare @text nvarchar(max)='Here, is my text'; select substring(@text,1,@limit)
Result :
'Here, is m'
You've got few functions to get the part of string:
LEFT[^]
RIGHT[^]
SUBSTRING[^]
If you want to find a character, to be able to get 'limit', use CHARINDEX[^] function togheter with one of above functions. See PATINDEX[^] too.
DECLARE @t = 'Here is my Text' SELECT LEFT(@t, CHARINDEX(' ', @t)) AS CuttedText
This should work for you.
Declare @text Nvarchar(500)='Here, is my text' Declare @limit int=10 Declare @CuttText Nvarchar(500) select @CuttText=Substring(@text,1,@limit) Declare @Spaceindex int,@Reverse Nvarchar(100) set @Reverse=Reverse(@CuttText) select @Spaceindex=Charindex(' ',@Reverse) select Reverse(Substring(@Reverse,@SpaceIndex+1,LEN(@CuttText)))
这篇关于如何在SQL中仅显示文本中的x个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文