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.
| Author |
Topic |
|
sree432
Starting Member
7 Posts |
Posted - 2004-11-19 : 04:03:41
|
In Items Table I Have ItemName Column Contains Following DataTable Name : ItemsColumn Name : ItemNameColumn Data :: Out Put Required========================================Cheese Balls :: CBMC Burger :: MBChicken Pizza :: CPPepsi To Pepsi :: PTPMC D Special Pizza :: MDSPPlease Help Me Out In Getting One 'Select Statement' OrOr How We Can Get ItEarly Thanks For Solution Provider Keep me smilingSR |
|
|
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) AsBeginDeclare @Acronym varchar(50)Declare @numbers Table (n int identity(1,1), blah int)Insert Into @numbers Select 1 from master.dbo.syscolumnsSelect @Acronym = isnull(@Acronym,'') + Z.letterFrom ( 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 ) ZReturn @AcronymEndgoCreate Table #myTable (Name varchar(100), abbrev varchar(50))Insert Into #myTableSelect 'Cheese Balls', nullUnion Select 'MC Burger', nullUnion Select 'Chicken Pizza', nullUnion Select 'Pepsi To Pepsi', nullUnion Select 'MC D Special Pizza', nullSelect name, Abbrev = dbo.Acronym(name)From #myTableDrop Table #myTablegoDrop Function dbo.AcronymCorey |
 |
|
|
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) AsBegin 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 @AcronymEnd - Jeff |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
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 |
 |
|
|
|
|
|
|
|