Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Function To Retrieve Last Index Expand / Collapse
Author
Message
Posted Sunday, October 7, 2007 8:42 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, April 14, 2014 6:47 AM
Points: 967, Visits: 450
Comments posted to this topic are about the item Function To Retrieve Last Index

Prasad Bhogadi
www.inforaise.com
Post #407778
Posted Monday, December 21, 2009 5:47 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 3:25 PM
Points: 2, Visits: 92
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

Post #837671
Posted Monday, July 25, 2011 9:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Post #1147596
Posted Tuesday, April 22, 2014 6:39 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 6:35 AM
Points: 1, Visits: 0
Check following script made little modification.

declare @ToFind varchar(100)
declare @InString varchar(200)

set @ToFind = 'llo'
set @InString = 'Hello World, Hello World'

select LEN(@InString) - (CharIndex(reverse(@ToFind),reverse(@InString)) + (len(@ToFind) -2))

Post #1563816
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse