0

I have a column with data like:

401-305-3069703-1---------------------------------------------------------
1-1499964-305-395---------------------------------------------------------
1-14995964-4405-395---------------------------------------------------------
....

How I can remove all dash and any character after last number like:

401-305-3069703-1
1-1499964-305-395
1-14995964-4405-395
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

4

You can try like following using PATINDEX and REVERSE

SELECT LEFT(@str, LEN(@str) - PATINDEX('%[0-9]%', REVERSE(@str)) +1)

Online Demo

Here PATINDEX is being used to find the last occurrence of any number using wildcard pattern, as there are multiple numbers and we want to find the last number index so we are reversing it using REVERSE before applying the PATINDEX.

Once the index is found, using LEFT we are getting the substring from the text.

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
PSK
  • 17,547
  • 5
  • 32
  • 43