CharIndex Reverse - find occurrence starting from end of string in TSQL

What?
This is a quick note on finding the last occurrence of a string in a longer string. This has to be in Transact SQL for a SQL Server instance only and not filtered by other code.


Why?
I have a string such as the following (column positions added for demo purposes):
copyraw
String1.String2.String3.String4
1   5   10   15   20   25   30 -> length = 31
  1.  String1.String2.String3.String4 
  2.  1   5   10   15   20   25   30 -> length = 31 
I'd like to end up with just the last part of this, ie "String4". So I need to delimit based on the dot/period (.) and use substring in a sort of reversed form.

For argument's sake, I'm assigning this string to the variable "haystack".


How?
Perhaps we should determine the position of the last needle first (reverse the haystack string and find needle):
copyraw
DECLARE @Haystack VARCHAR(31);
SET @Haystack = 'String1.String2.String3.String4';
-- I want "String4" from Haystack

SET @Delimiter = '.';

PRINT @Haystack;
-- yields "String1.String2.String3.String4"


SELECT REVERSE( @Haystack );
-- yields "4gnirtS.3gnirtS.2gnirtS.1gnirtS"


SELECT CHARINDEX(@Delimiter, REVERSE( @Haystack ))
-- yields "8"


SELECT SUBSTRING(@Haystack, CHARINDEX(@Delimiter, REVERSE(@Haystack)), LEN(@Haystack))
-- yields ".String2.String3.String4"
  1.  DECLARE @Haystack VARCHAR(31)
  2.  SET @Haystack = 'String1.String2.String3.String4'
  3.  -- I want "String4" from Haystack 
  4.   
  5.  SET @Delimiter = '.'
  6.   
  7.  PRINT @Haystack; 
  8.  -- yields "String1.String2.String3.String4" 
  9.   
  10.   
  11.  SELECT REVERSE( @Haystack )
  12.  -- yields "4gnirtS.3gnirtS.2gnirtS.1gnirtS" 
  13.   
  14.   
  15.  SELECT CHARINDEX(@Delimiter, REVERSE( @Haystack )) 
  16.  -- yields "8" 
  17.   
  18.   
  19.  SELECT SUBSTRING(@Haystack, CHARINDEX(@Delimiter, REVERSE(@Haystack)), LEN(@Haystack)) 
  20.  -- yields ".String2.String3.String4" 


Not quite right, as we got the last occurrence counting from the beginning rather than the end, so we still need to work out the starting point:
copyraw
SELECT  LEN(@Haystack) - CHARINDEX(@Delimiter, REVERSE(@Haystack))
-- yields "23"


SELECT SUBSTRING(@Haystack, LEN(@Haystack) - CHARINDEX(@Delimiter, REVERSE(@Haystack)), LEN(@Haystack))
-- yields "3.String4"
  1.  SELECT  LEN(@Haystack) - CHARINDEX(@Delimiter, REVERSE(@Haystack)) 
  2.  -- yields "23" 
  3.   
  4.   
  5.  SELECT SUBSTRING(@Haystack, LEN(@Haystack) - CHARINDEX(@Delimiter, REVERSE(@Haystack)), LEN(@Haystack)) 
  6.  -- yields "3.String4" 


Almost there, counting backwards and forwards it seems the index is just two characters off (as I don't want to include the period):
copyraw
SELECT LEN(@Haystack) - CHARINDEX(@Delimiter, REVERSE(@Haystack)) + 2
-- yields "25"


SELECT SUBSTRING(@Haystack, LEN(@Haystack) - CHARINDEX(@Delimiter, REVERSE(@Haystack)) + 2, LEN(@Haystack))
-- yields "String4"  Yay!!!
  1.  SELECT LEN(@Haystack) - CHARINDEX(@Delimiter, REVERSE(@Haystack)) + 2 
  2.  -- yields "25" 
  3.   
  4.   
  5.  SELECT SUBSTRING(@Haystack, LEN(@Haystack) - CHARINDEX(@Delimiter, REVERSE(@Haystack)) + 2, LEN(@Haystack)) 
  6.  -- yields "String4"  Yay!!! 


Done!

Untested but in theory:
copyraw
SELECT REVERSE( SUBSTRING( REVERSE( @Haystack ), 1, CHARINDEX( @Delimiter, REVERSE( @Haystack ) ) - LEN( @Delimiter ) ) )
  1.  SELECT REVERSE( SUBSTRING( REVERSE( @Haystack ), 1, CHARINDEX( @Delimiter, REVERSE( @Haystack ) ) - LEN( @Delimiter ) ) ) 
Category: Transact-SQL :: Article: 478

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2022 Joel Lipman .com. All Rights Reserved.