Click here to Skip to main content
15,909,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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!
Posted
Comments
King Fisher 6-Nov-14 7:30am    
so, what's you Result ?
Rajesh waran 6-Nov-14 7:49am    
what you are expecting,your question is not clear. your limit size is 10 but you are expecting output as only 8. here, what do you mean by "without break the words"

Try this substring inSql


SQL
declare @limit int=10;
declare @text nvarchar(max)='Here, is my text';
select substring(@text,1,@limit)



Result :

'Here, is m'
 
Share this answer
 
v2
Comments
feliciana 6-Nov-14 7:38am    
I don't want to break words. For @limit=10 the result i want to be: @text='Here, is'
King Fisher 6-Nov-14 7:43am    
what do you mean break Words ? :)
feliciana 6-Nov-14 8:03am    
I want the words to be complete. If i have @limit=10 characters from @text='Here, is my text', the first 10 characters will be 'Here, is m'. So in this case I want to show only 'Here, is'.
King Fisher 6-Nov-14 8:22am    
Then make you limit as 8.
feliciana 6-Nov-14 8:32am    
You're right for this example, but if i have in a table a column that contain text with different size and words with different size, how can i do that in this case. This is my problem.
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.

SQL
DECLARE @t = 'Here is my Text'
SELECT LEFT(@t, CHARINDEX(' ', @t)) AS CuttedText
 
Share this answer
 
v2
This should work for you.

SQL
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)))
 
Share this answer
 
Comments
feliciana 6-Nov-14 8:37am    
Thank you, this is what i want, it's working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900