Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Abbreviate words
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

United Kingdom
207 Posts

Posted - 01/05/2013 :  17:43:03  Show Profile  Reply with Quote
Hi there,

I have a field that should be restricted to max 40 characters (this includes spaces).

If the field string contains over 40 characters, I want to abbreviate the words rather than trimming the end.

So, ideally I would like to abbreviate all words in the field to three characters.

For example
Johnny found an apple on the ground below

Expected Result:
Joh fou an app on the gro bel

I know this looks mad but it's the only viable way for me to do this, and with so much data.

Any ideas?


Starting Member

35 Posts

Posted - 01/05/2013 :  19:29:03  Show Profile  Reply with Quote
You could create an insert and updated trigger which processes the data before the insert.

--here comes the code to process the string if it is longer than 40 characters before inserting the data within the table

and create also the same trigger also for update statements (rename to INSTEAD OF UPDATE)

An example would be the following:
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 01/06/2013 :  10:17:29  Show Profile  Reply with Quote
so what if length still goes beyond 40 even after trimming?

SQL Server MVP

Go to Top of Page

Posting Yak Master

United Kingdom
226 Posts

Posted - 01/07/2013 :  04:20:02  Show Profile  Visit theboyholty's Homepage  Reply with Quote
From a performance point of view, the following code won't win anyone any awards, however needs must and it should do the job. As you said, the remit looks a bit mad but the following code will take any string (up to 100 characters), reduce each individual character fragment within that string to a maximum of three characters and also limit the final output to 40 characters.
Its a RBAR approach, as opposed to set-based, so if anyone can improve on it, I would be interested to see their work.

create function fn_TruncateWords (@string varchar(100))
returns varchar(40)

declare @c int = 1, @space int = (LEN(@string)-LEN(replace(@string,' ','')))+1

declare @fragment varchar(20), @newstring varchar(100)
while @c <= @space
	set @fragment = case when SUBSTRING(@string,0,CHARINDEX(' ',@string,0)) <> '' then SUBSTRING(@string,0,CHARINDEX(' ',@string,0)) else @string end
	set @string = REPLACE(@string,SUBSTRING(@string,0,CHARINDEX(' ',@string,0))+' ','')
	set @newstring = isnull(@newstring,'')+left(@fragment,3)+' '
	set @c += 1

return @newstring

select dbo.fn_TruncateWords ('Johnny found an apple on the ground below')

--------------------------------------------------------------------------------- A Bury FC supporters website and forum

Edited by - theboyholty on 01/07/2013 04:21:09
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 01/07/2013 :  10:12:31  Show Profile  Reply with Quote
I dont think the approach itself is proper way to deal with problem at hand.

SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000