This should work for variable length prefixes and suffixes:set nocount ondeclare @tb table (ID varchar(10))insert @tb Select 'a12345' union allselect 'ab12345' union allselect 'abc12' union allselect 'abcde1' union allselect '123' union allselect null union allselect 'abcd' union allselect 'abc231a' union allselect ''select convert(int, case when patix > 0 then right([ID], patix) when patix = -1 and length > 0 then [ID] else null end)From ( select [ID] ,patindex('%[^0-9]%', reverse([ID]))-1 patix ,length = len([ID]) from @tb ) aBe One with the OptimizerTG