0

I want to get number from String in sql Example: mn21 or mnp100, how to get number from "mb21" or "mnp100". Then, i will sort order by numeric

2
  • Do all strings end in a number? Commented Apr 16, 2015 at 1:19
  • yes, but they have different lenght Commented Apr 16, 2015 at 1:21

1 Answer 1

4

You can use PATINDEX to search for the start index of the number and then RIGHT to get the remaining numbers:

WITH SampleData(string) AS(
    SELECT 'mn21' UNION ALL
    SELECT 'mnp100'
)
SELECT
    string, 
    Number = CAST(RIGHT(string, LEN(string) - PATINDEX('%[0-9]%', string) + 1) AS INT)
FROM SampleData
ORDER BY Number
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.