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 2000 Forums
 SQL Server Development (2000)
 Join The First Letter Of Each Word

Author  Topic 

sree432
Starting Member

7 Posts

Posted - 2004-11-19 : 04:03:41
In Items Table I Have ItemName Column Contains Following Data
Table Name : Items
Column Name : ItemName
Column Data :: Out Put Required
========================================
Cheese Balls :: CB
MC Burger :: MB
Chicken Pizza :: CP
Pepsi To Pepsi :: PTP
MC D Special Pizza :: MDSP

Please Help Me Out In Getting One 'Select Statement' Or
Or How We Can Get It

Early Thanks For Solution Provider
Keep me smiling

SR

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-11-19 : 08:51:38
try this out... its one select (using a function )


Create function dbo.Acronym
(@name varchar(100))
Returns varchar(50) As
Begin
Declare @Acronym varchar(50)

Declare @numbers Table (n int identity(1,1), blah int)
Insert Into @numbers
Select 1 from master.dbo.syscolumns

Select @Acronym = isnull(@Acronym,'') + Z.letter
From
(
Select top 100 percent letter = substring(' '+A.name,B.n+1,1)
From (Select name = @name) A
Inner Join @numbers B
On substring(' '+A.name,B.n,1) = ' '
and B.n < len(A.name)+1
Order By B.n
) Z

Return @Acronym
End
go
Create Table #myTable (Name varchar(100), abbrev varchar(50))
Insert Into #myTable
Select 'Cheese Balls', null
Union Select 'MC Burger', null
Union Select 'Chicken Pizza', null
Union Select 'Pepsi To Pepsi', null
Union Select 'MC D Special Pizza', null

Select
name,
Abbrev = dbo.Acronym(name)
From #myTable

Drop Table #myTable
go
Drop Function dbo.Acronym


Corey
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-11-19 : 09:04:12
Corey -- i think the overhead of creating a numbers table into a table variable, and then executing a SELECT with an execution plan and all that will be pretty inefficient for something this simple; especially, since it is being done with each call to the UDF. If you do somethhing similiar in set-based manner to the entire table at once, it might be worth it, but to call a UDF like this for each row in a large table might not be the best way to get it done.

In cases like this, the most efficient solution will most likely be something simple like:


Create function dbo.Acronym (@name varchar(100))
Returns varchar(50) As
Begin
Declare @Acronym varchar(50);
declare @i int;

select @name = ' ' + rtrim(@name), @i=2, @Acronym = ''

while (@i < len(@name))
select @Acronym= @Acronym +
case when substring(@name,@i-1,1) = ' '
then substring(@name,@i,1) else '' end,
@i=@i+1

Return @Acronym
End





- Jeff
Go to Top of Page

lazerath
Constraint Violating Yak Guru

343 Posts

Posted - 2004-11-19 : 10:49:06
Just for giggles, I tested both solutions. I modified Corey's to use a pre-existing numbers table, but the results definately point to Dr. Cross Join's udf (once a missing a ')' is supplied at the end of the while). On my system, the looping method performed roughly 8 times better than the set based method.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-11-19 : 12:01:36
yeah, tally table solutions are good, but you have to use them in the right places. Never forget that looping through a STRING stored in memory (which has access time measured in nano-seconds) is very different from looping through ROWS in a table (which might require I/O).

So, in this case, it's not really set-based versus procedural, since we are operating on a single value (i.e., row). The "set-based" solution actually complicates it by creating multiple rows for no reason !

- Jeff
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-11-19 : 14:47:57
Very good point! I see what you mean... I claim sleepiness

Corey
Go to Top of Page
   

- Advertisement -