Skip to main content

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-GHI10” and also “GHJ23”. 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(PARSENAME(modifiedName, 3) AS  nvarchar ),
                       CAST(PARSENAME(modifiedName, 2) AS  INT ),         
                       CAST(dbo.fFilterNumeric (PARSENAME(modifiedName, 1)) AS  INT ) 
                     ) AS sorted_name 
  FROM (SELECT [name],
               REPLACE([Name], '-', '.') AS modifiedName        
          FROM myTestTable) AS myTestTableSorted 

OK the next thing is to explin what I have done here. First PARSENAME gets the string part as for the index from the end of the string. Since the PARSENAME is working only with '.' I had to replace the '-' first. Then for the last part it should be sorted as for the numeric part. For that I have found a function written by IndianScorpion on forums.asp.net to do that.

CREATE FUNCTION [dbo].fFilterNumeric (
       @Src NVARCHAR (255)) 
       RETURNS NVARCHAR (255) 
       AS 
       BEGIN 
           declare @Res nvarchar(255) 
           declare @i int, 
           @l int, 
           @c char 
           select @i=1, 
                  @l=len(@Src) 
           SET @Res = '' 
           while @i<=@l 
              begin    
                  set @c=upper(substring(@Src,@i,1))     
                  IF  isnumeric(@c)=1         
                       SET @Res = @Res + @c    
                       set @i=@i+1 
              end   
           return(@res) 
        END  

Comments

Popular posts from this blog

Google API v3 with PHP using Blogger service

It was really hard for me to understand how the Google APIs are working at the first point and took few days for me to figure out. But after a successful working prototype it seems very easy. And also when I am searching for a simple example I was unable to find a good one that I can understand. So let me list down step by step what I have done with URLs and as simple as I can. Create a Google app location -  https://code.google.com/apis/console Switch on the "Blogger API v3" Get the latest APIs client library for PHP location -  https://code.google.com/p/google-api-php-client/downloads/list Upload the files to your host location on on localhost Extract the files to folder  named "GoogleClientApi" Create your php file outside of the folder  Copy paste following code into the file and do the changes as needed  By changing the scope and the service object you can access all the services that is given by Google APIs through the PHP API library se

My two cents on new year resolution

What is the plan for the new year ? - need to think on what are we trying achieve during next year 2018 - basically the life goals - may be personal or professional - and also it should be realistic (not something like going to Mars ;)) Why we need a plan for the new year ? - basically a goal without a plan is a DREAM - And also should be able to measure (what you cannot measure, you cannot manage) How to prepare a new Year resolution/plan ? - Leave some buffer time - Make changes during the year (life is changing/evolving) - Plan is only for you (do not share it) - When a milestone is achieved, celebrate - Try to stick to the plan - otherwise no point of planing

Assets and Liabilities as Rich Dad, Poor Dad explains

I was reading "The rich dad poor dad by Robert Kiyosaki" here is a one point that he mentions on that. Basically Asset as he says is little bit different than on books. If something puts money in your pocket it is a asset. And Liabilities are the ones that takes money out of your pocket. OK for example a house or a car may seems like an Asset but it takes money out of you pocket to maintain them. But if you rent them or make them to make money at the end of the day you can convert it to a asset. Basically that what rich people do. They buy assets. Middle class buy liabilities (thinking those are assets) and stuff (a lot of them that not used or that not needed). Lower class buy to consume (basic needs like foods).