How to remove everything before a certain character in SQL Server? How to remove everything before a certain character in SQL Server? sql sql

How to remove everything before a certain character in SQL Server?


Try this:

right(MyColumn, len(MyColumn) - charindex('-', MyColumn))

Charindex finds the location of the hyphen, len finds the length of the whole string, and right returns the specified number of characters from the right of the string.


may be the other way you can do it by using reverse and Char Index

DECLARE  @Table1 TABLE     (val varchar(10));INSERT INTO @Table1    (val)VALUES('ABC-123'),    ('AB- 424'),    ('ABCD-53214')select reverse(substring(reverse(val),0,CHARINDEX('-',reverse(val)))) from @Table1