Skip to main content

Posts

Showing posts from March, 2009

Sort Fields which contains both Numeric and Characters

It was a long time since my last post. Finally I got something to write about. Store Procedures … something I hate most of the time. But today I felt good about them because I was able to find a solution for a sorting issue. The particular column contained data like “98ABC-76543-DEF-12345-GHI 10 ” and also “GHJ 23 ”. I was supposed to sort this field on Numeric value for the last part in red . And for the other parts it should be treated as Numeric for the Numeric parts and as Strings for the others. So what should I do was to split all parts and sort them separately and get numeric part in red and sort. But wait a minute do we have to all the things??? I don’t think so... I have found ( actually one of my friends ) an easy way to do that. Here it is. SELECT [name], RANK() OVER (ORDER BY CAST(PARSENAME(modifiedName, 5) AS nvarchar ), CAST(PARSENAME(modifiedName, 4) AS INT ), CAST