devxlogo

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Seasoned Architects Evaluate New Tech

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.