|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, May 02, 2013 5:00 AM
Points: 967,
Visits: 447
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: 2 days ago @ 11:05 AM
Points: 2,
Visits: 70
|
|
I didn't compare head to head, but I would think something like this might perform better with longer strings:
-- NOTE: this uses the len function, so this function ignores whitespace at end also
CREATE FUNCTION dbo.LastIndexOf( @char as char , @string as varchar(max) ) RETURNS integer AS BEGIN SET @string = rtrim(@string)
if len(@string) = 0 return 0
DECLARE @revString as varchar(max) DECLARE @lastIndex as integer SET @revString = reverse(@string) SET @lastIndex = charIndex(@char, @revString) -- find the last char in the reverse string IF @lastIndex = 0 return 0
SET @lastIndex = len(@string) - @lastIndex + 1 -- flip it so counting from the front
RETURN @lastIndex END
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, July 26, 2011 6:19 AM
Points: 6,
Visits: 9
|
|
I'm not sure a function is required for this. Have you considered:
declare @ToFind varchar(10) declare @InString varchar(20)
set @ToFind = 'llo' set @InString = 'Hello World, Hello World'
select LEN(@InString) - CharIndex(reverse(@ToFind),reverse(@InString)) - 1 This returns: 16
|
|
|
|