devxlogo

Order by

Order by

Question:
I can’t sort by the second-last digit of a number (for example, sorting the numbers 4491, 1195, and 1364 by the third digit).

Answer:
Try using the “RIGHT” function to order the numbers. If they are stored as integers, you will first have to convert them to character. Here’s the code I used:

use scratchgocreate table #test(	x int)insert into #test values(4491)insert into #test values(1195)insert into #test values(1364)insert into #test values(111)select xfrom #testorder by right(cast(x as char(8)),2)drop table #test

devx-admin

Share the Post: