SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

cidr
Posting Yak Master

United Kingdom
203 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?

Thanks

ovc
Starting Member

Romania
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.

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

India
52309 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
http://visakhm.blogspot.com/

Go to Top of Page

theboyholty
Posting Yak Master

United Kingdom
221 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)
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

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

visakh16
Very Important crosS Applying yaK Herder

India
52309 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
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.09 seconds. Powered By: Snitz Forums 2000