How to Extract a Substring From a String in T-SQL

  • Hi SQL Team,

    May I know how to get the 'ABC123_Clm R' string from the following string in SQL Server? Many thanks.

    /Opr Rep/Clm R/ABC123_Clm R

    • This topic was modified 1 year, 8 months ago by  tim.hortons.
  • This is just the same solution as I use in Extract only the filename from a file path; get the right most characters of the string, using the location of the first / character in the reversed string. I also, however, concatenate an extra / character to avoid an error, which I didn't do in the prior solution:

    DECLARE @YourString varchar(50) = '/Opr Rep/Clm R/ABC123_Clm R';

    SELECT @YourString AS FilePath,
    RIGHT(@YourString, CHARINDEX('/', REVERSE(@YourString)+'/') -1) AS YourValue;

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • DECLARE @input_string VARCHAR(100) = '/Opr Rep/Clm R/ABC123_Clm R'

    SELECT RIGHT(@input_string, CHARINDEX('/', REVERSE('/' + @input_string)) - 1) AS e_string

  • This was removed by the editor as SPAM

  • This was removed by the editor as SPAM

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply