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

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

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.

About Our Journalist