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

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Abbreviate words

Author  Topic 

cidr
Posting Yak Master

207 Posts

Posted - 2013-01-05 : 17:43:03
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?

Thanks

ovc
Starting Member

35 Posts

Posted - 2013-01-05 : 19:29:03
You could create an insert and updated trigger which processes the data before the insert.

CREATE TRIGGER name ON table
INSTEAD OF INSERT
AS
--here comes the code to process the string if it is longer than 40 characters before inserting the data within the table
END

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

An example would be the following:
http://weblogs.asp.net/ryanw/archive/2006/02/02/437242.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-06 : 10:17:29
so what if length still goes beyond 40 even after trimming?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

theboyholty
Posting Yak Master

226 Posts

Posted - 2013-01-07 : 04:20:02
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)
as
begin

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

declare @fragment varchar(20), @newstring varchar(100)
while @c <= @space
begin
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
end

return @newstring
end



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

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk A Bury FC supporters website and forum
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

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

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -