If you are looking for following solution than probably this post can help you out:
Note: The Example I am going to discuss is the solution for above mention criteria, understand it and you can be able to derive solution for your problem. I want to sort a string like this. Nodes1-49 100-249 1000-2499 10000+ 250-499 2500-4999 50-99 500-749 5000-9999 750-999 Step1: To undergo this task I need to extract characters from left till this "-" or "+" character. Let find length till this "-" select distinct MyExpr,charindex('-',MyExpr)-1 as Nodes from MyTable
So I also require separate statement for select distinct MyExpr,charindex('-',MyExpr)-1 as Nodes from MyTable
Now lets extract part of string by using substring function and case select statement. select distinct MyExpr as Nodes, substring(MyExpr,1,( case when charindex('-',MyExpr)-1 = '-1' then charindex('+',MyExpr)-1 else charindex('-',MyExpr)-1 end ) ) as 'NodesSort' from MyTable Order by 'NodesSort'
So this gives us extraction of Part of string from string. You can also see trim right portion of string.
Casting string to int and applying order by for sorting. select distinct MyExpr as Nodes, -- SORT Logic cast( substring(MyExpr,1,( case when charindex('-',MyExpr)-1 = '-1' then charindex('+',MyExpr)-1 else charindex('-',MyExpr)-1 end ) ) as int) as 'NodesSort' from MyTable order by 'NodesSort'
|
Collection and sharing of, interview questions and answers asked in various interviews, faqs and articles.....
No comments:
Post a Comment