-3

How can i extract the far right digits out of these strings?

Appointment Regional Sales Manager changed from 5 to 6
Appointment Regional Sales Manager changed from to 8
Appointment Regional Sales Manager changed from 6 to 15
Appointment Regional Sales Manager changed from 11 to 16

w00t
  • 29
  • 7
  • 2
    Have you tried anything at all? Hint: String functions are your friend. We will, however, not write the code for you if you show no effort whatsoever. We love helping people who show initiative, but we're not your private army of code monkeys that will do your (home)work for you. – SchmitzIT Feb 15 '17 at 10:11
  • 3
    I'm voting to close this question as off-topic because it shows no sign of any effort by the author – SchmitzIT Feb 15 '17 at 10:13
  • 1
    @SchmitzIT: Poor quality question are not neccesarily off topic, they just need some editing. – Zohar Peled Feb 15 '17 at 10:17
  • 1
    Possible duplicate of [Query to get only numbers from a string](http://stackoverflow.com/questions/16667251/query-to-get-only-numbers-from-a-string) – Darshak Feb 15 '17 at 10:18
  • @ZoharPeled - IIRC there was a close-reason for that in the past. I couldn't find it, though. I ended up on the free-text off-topic choice. – SchmitzIT Feb 15 '17 at 10:25
  • 1
    @SchmitzIT. That reason was removed, because a poor question is not an invalid question. Poor questions are suitable for downvoting, but are still valid questions. See also http://meta.stackoverflow.com/questions/271863/misuse-of-off-topic-closure-reason/272028 – PaulG Feb 15 '17 at 10:59

1 Answers1

1

One way to do it is using patindex, reverse, and right.

Create and populate sample table(Please save us this step in your future questions):

DECLARE @T AS TABLE
(
    col1 varchar(100)
)

INSERT INTO @T VALUES
('Appointment Regional Sales Manager changed from 5 to 6'),
('Appointment Regional Sales Manager changed from to 8'),
('Appointment Regional Sales Manager changed from 6 to 15'),
('Appointment Regional Sales Manager changed from 11 to 16')

The query:

SELECT RIGHT(col1, PATINDEX('%[^0-9]%', REVERSE(col1))-1) As LastDigits
FROM @T

Results:

LastDigits
6
8
15
16
Graham
  • 7,431
  • 18
  • 59
  • 84
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121