0

this is how it is currently sorted:

CRI-SU1
CRI-SU10
CRI-SU11
CRI-SU2
CRI-SU3
CRI-SU4
CRI-SU5
CRI-SU6
CRI-SU7
CRI-SU8
CRI-SU9

I wanted it to be sorted numerically like:

CRI-SU1
CRI-SU2
CRI-SU3
CRI-SU4
CRI-SU5
CRI-SU6
CRI-SU7
CRI-SU8
CRI-SU9
CRI-SU10
CRI-SU11

How do you sort it in SQL? this is my sql statement...

SELECT systemUnitID FROM systemUnit ORDER BY systemUnitID
1
  • What's the specific database? The answer depends on the available database functions. Commented Sep 19, 2018 at 0:31

2 Answers 2

2

If the prefix always reads "CRI-SU", you can try to replace it with the empty string by using replace(). Then cast the result to an integer with cast() and order by the casted value. This should work in many DBMS.

SELECT systemunitid
       FROM systemunit
       ORDER BY cast(replace(systemunitid, 'CRI-SU', '') AS integer);
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT systemUnitID 
FROM systemUnit
ORDER BY CAST(REPLACE(systemUnitID, 'CRI-SU', '') As int)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.