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
Asked
Active
Viewed 402 times
0
-
Do all strings end in a number? – Felix Pamittan Apr 16 '15 at 01:19
-
yes, but they have different lenght – Bader Apr 16 '15 at 01:21
1 Answers
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

Felix Pamittan
- 31,544
- 7
- 41
- 67