

If you inspect this definition, you’ll see there is a lot of program logic.
#Sort numeric fields aquamacs how to#
Hopefully, you leaned some more about sorts, and also how to use PATINDEX, LEFT, and SUBSTRING to manipulate character values. It would be hard to teach, and I wanted you to learn something. The UDF is pretty complicated and very advanced.ORDER BY dbo.fn_CreateAlphanumericSortValue(section)Īt this point, you may be saying hey! Why did you go through that first example and all those functions instead of just showing us this general case and the UDF? Using our example, you could use the UDF to sort as: Later on, I will teach you how to create and define UDFs, but since this article really provides a great general solution to the problem, I wanted to include it as part of this article, so those looking for a really workable solution would know of it, and be able to use it. Once you read the article, you’ll see how you can define a UFD called fn_CreateAlphanumericSortValue, which you can then call from your SQL.
#Sort numeric fields aquamacs code#
This article is really advanced, and the code complicated, but the short of the matter is that you can create a UDF (User Defined Function) to create the alpha numeric sort value. I recently came across a really good article by Craig Finck on Alphanumeric Sorting In MSSQL. What about values such as MI10.5AB23 or MI200.5AB500? Is there an easy way to sort these values alphanumerically? General Case for Sorting Alphanumeric Values However, as you have noticed, this example only works when the alphanumeric value consists of one alpha and one numeric section. Hopefully through this example, you’ve see that it is possible to construct a customized sort expression to sort columns alphanumerically. The table sorted with the built-in sort, and.When you run this code, you’ll see three results: ORDER BY LEFT(Section,PATINDEX('%%',Section)-1), - alphabetical sortĬONVERT(INT,SUBSTRING(Section,PATINDEX('%%',Section),LEN(Section))) - numerical sort INSERT INTO dbo.Section (Section.Section) VALUES ('A10') INSERT INTO dbo.Section (Section.Section) VALUES ('11') INSERT INTO dbo.Section (Section.Section) VALUES ('B32') INSERT INTO dbo.Section (Section.Section) VALUES ('B1') INSERT INTO dbo.Section (Section.Section) VALUES ('2') INSERT INTO dbo.Section (Section.Section) VALUES ('AB100') INSERT INTO dbo.Section (Section.Section) VALUES ('B3') INSERT INTO dbo.Section (Section.Section) VALUES ('AB10') INSERT INTO dbo.Section (Section.Section) VALUES ('B21') INSERT INTO dbo.Section (Section.Section) VALUES ('B20') INSERT INTO dbo.Section (Section.Section) VALUES ('A11')

INSERT INTO dbo.Section (Section.Section) VALUES ('B2') INSERT INTO dbo.Section (Section.Section) VALUES ('A1') INSERT INTO dbo.Section (Section.Section) VALUES ('AB1') INSERT INTO dbo.Section (Section.Section) VALUES ('1') To get the alpha portion, we use the following expression: Using the diagram above, you can see that everything to the left of the number is a character.

