-1

Can someone tell me how to find the character index from the end?

Example:

John(Jk)

I want to find the ( from the end.

Result:

4
jarlh
  • 42,561
  • 8
  • 45
  • 63
Aravindhan R
  • 270
  • 5
  • 26

3 Answers3

3

One way to rephrase your question is that you want to find the first occurrence of ( from the reversed string.

SELECT
    LEN(col) - CHARINDEX('(', REVERSE(col)) + 1
FROM yourTable;

Demo

In the above demo, the char index of the final ( in the string John(Jk) correctly is determined to be 5.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Use charindex() with reverse() function to find the index from the end

select charindex('(', reverse(data)) from table
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
-1

You can use CHARINDEX.

Syntax:

CHARINDEX ( expressionToFind , expressionToSearch [ , start_location ] )   

Best Regarads!

Kevin Böhmer
  • 462
  • 4
  • 21
  • How do you know where to start searching from the beginning? How does this help the OP? – Tim Biegeleisen Jan 08 '18 at 08:22
  • I am not intending to spoon-feed anyone here, so i linked a page where you can simply read everything you need to know about Charindex(). Remember that we are not here to solve his homework. – Kevin Böhmer Jan 08 '18 at 08:28