﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Prasad Bhogadi  / Function To Retrieve Last Index  / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 14:44:53 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Function To Retrieve Last Index</title><link>http://www.sqlservercentral.com/Forums/Topic407778-710-1.aspx</link><description>I'm not sure a function is required for this. Have you considered:[code="sql"]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[/code]This returns: 16</description><pubDate>Mon, 25 Jul 2011 09:31:13 GMT</pubDate><dc:creator>jonegerton</dc:creator></item><item><title>RE: Function To Retrieve Last Index</title><link>http://www.sqlservercentral.com/Forums/Topic407778-710-1.aspx</link><description>I didn't compare head to head, but I would think something like this might perform better with longer strings:[code="other"]-- NOTE: this uses the len function, so this function ignores whitespace at end alsoCREATE FUNCTION dbo.LastIndexOf(	@char as char	, @string as varchar(max))RETURNS integerAS 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  @lastIndexEND[/code]</description><pubDate>Mon, 21 Dec 2009 17:47:36 GMT</pubDate><dc:creator>shannonholck</dc:creator></item><item><title>Function To Retrieve Last Index </title><link>http://www.sqlservercentral.com/Forums/Topic407778-710-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/T-SQL+Aids/31116/"&gt;Function To Retrieve Last Index &lt;/A&gt;[/B]</description><pubDate>Sun, 07 Oct 2007 08:42:19 GMT</pubDate><dc:creator>Prasad Bhogadi</dc:creator></item></channel></rss>