Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optimize this function 3

Status
Not open for further replies.

Bertramp

Technical User
Dec 12, 2006
11
GB
Is there anyway to optimize this function:

CREATE function strip(@para varchar(110))
returns varchar(110)
as
begin
declare @i varchar(110)
if charindex('/',@para) > 0
set @i = left(@para,charindex('/',@para)-1)
else
Set @i = @para
return @i
end

The objective is to strip any characters after and including the '/' - e.g 'test/query' should return 'test' and any string not having the '/' character should be left as is
 
Your function can be re-written as...

Code:
CREATE function strip(@para varchar(110))
returns varchar(110)
as
  begin

    Return(Select Case When CharIndex('/', @para) > 0 
                       Then Left(@Para, CharIndex('/', @para) - 1)
                       Else @Para
                       End)
  End

For even better speed improvements, put this case syntax directly in to the query without using a function.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
This is what I get
Code:
CREATE function strip(@para varchar(110))
returns varchar(110)
as
   begin
      return substring(@para, 1, case charindex('/',@para)
      when 0 then len(@para) else (charindex('/',@para))
      end)   
   end





<.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top