Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove substring before last occurrence of a special character in Sql server

Tags:

sql-server

I want to remove the substring before last occurrence of period(.). The Query should convert r.k.Lee Brown to Lee Brown. So, basically I need the substring before last dot . and replace it with ''.

like image 743
User 4989588 Avatar asked Sep 03 '25 15:09

User 4989588


1 Answers

Try this:

SELECT RIGHT(@str, CHARINDEX('.', REVERSE(@str)) - 1)

You can slightly modify the above using:

REVERSE('.' + @str))

instead of

REVERSE(@str)

just in case there are no '.' in @str.

like image 156
Giorgos Betsos Avatar answered Sep 05 '25 13:09

Giorgos Betsos