How can I remove numbers after any word?
Here's some examples of what I'm trying to achieve:
RG12584WY1WLDZA9 -> RG12584WY1WLDZA
RG12584WY1WLDZA95 -> RG12584WY1WLDZA
CodePudding user response:
Your requirement would be easily handled by either a regex substring or regex replacement function, but SQL Server has no native support for either. One workaround is to use the PATINDEX
function on the reversed string to find the first non numerical index. Then, substring that and reverse again to get the result you want.
SELECT val, REVERSE(SUBSTRING(REVERSE(val), PATINDEX('%[A-Z]%', REVERSE(val)), LEN(val))) AS val_out
FROM t;