1

I have the following rows returned from my SQL query- can someone please help with a generic sorting code to sort the values in ascending order?

Also please note that my rows will be dynamically returned and will not always contain the same string as posted in my question above. It will be a mix of alphabets/integers. Below is just an example of a sample rows returned- need a generic formulae/sql approach and NOT a hard coding approach..thanks

('High_Speed'),
('M1 Speed'),
('M13 Speed'),
('M14 Speed'),
('M2 Speed'),
('M3 Speed'),
('Medium_Speed'),
('Test1 zone1 High_Speed'),
('Test1 zone11 High_Speed'),
('Test1 zone2 High_Speed'),
('Test1 zone21 High_Speed'),
('Zone206 Speed')

expected sorting-

('High_Speed'),
('M1 Speed'),
('M2 Speed'),
('M3 Speed'),
('M13 Speed'),
('M14 Speed'),
('Medium_Speed'),
('Test1 zone1 High_Speed'),
('Test1 zone2 High_Speed'),
('Test1 zone11 High_Speed'),
('Test1 zone21 High_Speed'),
('Zone206 Speed')
Rajeev
  • 27
  • 5

1 Answers1

1

Try this:

select t_col from (
select t_col
       , LEFT(SUBSTRING(t_col, PATINDEX('%[a-z]%', t_col), LEN(t_col))
         , PATINDEX('%[^a-z]%', SUBSTRING(t_col, PATINDEX('%[a-z]%', t_col), LEN(t_col)))-1) col_col
       , convert(int, LEFT(SUBSTRING(t_col, PATINDEX('%[0-9]%', t_col), LEN(t_col))
         , PATINDEX('%[^0-9]%', SUBSTRING(t_col, PATINDEX('%[0-9]%', t_col), LEN(t_col)))-1)) as ord
from test) T1
order by col_col asc, ord asc, t_col asc;

Here is a demo

VBoka
  • 8,995
  • 3
  • 16
  • 24
  • thanks however if you see, m13,m14 still come before m2,m3. Requirement is like m1,m2,m3,m13,m14, – Rajeev Jan 27 '20 at 05:21
  • Hello @Rajeev, i was probably tired when I was posting this because your question is clear. I have updated my answer and I hope it will be ok now... Cheers – VBoka Jan 27 '20 at 06:26
  • Hello @VBoka , thanks as I can see it is working now- just wanted to check whether the above code is SPECIFICALLY for the data I have maintained OR it is a GENERAL code that will work with any alphanumeric data? – Rajeev Jan 27 '20 at 07:09
  • Hi @Rajeev , you are welcome. Happy to help. I have wrote this code to work on your examples but I am not sure are there some string + number examples when this will not work... Sorry. If you find them do contact me to check them and try to give you a new code... Also, you can find different functions that you can create and then use this functions in your code(function to get only numbers from string, function to get only characters from string) and then order by results.... If this works for you I would appreciate if you can mark the answer as corect... Thanks! Cheers! – VBoka Jan 27 '20 at 07:24
  • Hi @VBoka, thanks will connect with you. Actually my data set can be varied containing alphanumeric strings; and their lengths would be different, hence wanted to check with you. Because above code gives me the following error for the dataset (m1 speed,m13 speed,m14 speed,m2 speed, m3 speed, zone206 speed)that I've,...Invalid length parameter passed to the LEFT or SUBSTRING function.... – Rajeev Jan 27 '20 at 07:45
  • Please can you use the above demo to demonstrate your problem ? I will try to help. Thanks! – VBoka Jan 27 '20 at 07:47