0

I have the following table with (man-made) IDs.

ID       Name
AB12345  John
12346    Charles
...

How do I write a SELECT that returns only the number segment of the ID column? Like this:

ID       Name
12345    John
12346    Charles
...
user256890
  • 3,396
  • 5
  • 28
  • 45
  • make a scalar function the loops through each character and concatenates character to string value only if it is BETWEEN '0' AND '9' – Cato Oct 19 '16 at 08:43

2 Answers2

1

You could write a regex to extract the numeric values that you are looking for.

This might help

Query to get only numbers from a string

Community
  • 1
  • 1
ChrisBint
  • 12,773
  • 6
  • 40
  • 62
1
SELECT SUBSTRING(string, PATINDEX('%[0-9]%', string), PATINDEX('%[0-9][^0-9]%', string + 't') - PATINDEX('%[0-9]%', 
                    string) + 1) AS Number
FROM table

here string is equal to id

Chanukya
  • 5,833
  • 1
  • 22
  • 36