Solved

Sorting Strings Numerically

Posted on 2006-11-06
6
654 Views
Last Modified: 2013-12-09
I have a field in my database that I need to sort. The field is a string but I need to sort numerically.

Version=9.5;Prior=NONE;
Version=9.2a;Prior=NONE;
Version=13.3;Prior=USS;
Version=13.1b;Prior=UAN;

How can I grab the version number and sort it so instead of:

9.5
9.2a
13.3
13.1b

I get:

13.3
13.1b
9.5
9.2

Someone suggested to me:

OK, here's one way...

Example "Builds" table:

Build
------
9.5
9.2a
13.3
13.1b
2.0
2.0a
2.0b
2.1
2.1a
2.1b

Querying the Builds table, sorting by the major build value:

SELECT
     Build,
     CAST(PARSENAME(Build,2) AS INT) AS MajorVersion,
     PARSENAME(Build,1) AS MinorVersion
FROM Builds
ORDER BY MajorVersion, MinorVersion

Outputs the following:

Build      MajorVersion MinorVersion
---------- ------------ ------------
2.0        2            0
2.0a       2            0a
2.0b       2            0b
2.1        2            1
2.1a       2            1a
2.1b       2            1b
9.2a       9            2a
9.5        9            5
13.1b      13           1b
13.3       13           3

I am using Firebird not SQL Server.

I am low on points but need help please. Thanks!
0
Comment
Question by:yourbudweiser
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:NickUpson
ID: 17884712
what version of firebird are you using?
0
 
LVL 3

Author Comment

by:yourbudweiser
ID: 17884729
Hi NickUpson, we are using Firebird 1.5
0
 
LVL 19

Expert Comment

by:NickUpson
ID: 17884790
I don't have time to write the code but I can give you the information necessary for you to do it

create an additional field in the table to hold the numeric form, say vernum
write an stored procedure that uses substring to step through the field, 1 char at a time until hits a non-numeric (skip/handle decimal point) building up the numeric form and save it.
then select and sort by the numeric form

if you are going to need it again, put the code in an trigger so that vernum is kept updated as data is updated/inserted
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 3

Author Comment

by:yourbudweiser
ID: 17884848
that is the general idea I received above but I am not sure how to extract just the version number from the string and I am not experienced to write stored procedures. Is there no quick and dirty way to do this in the sql statement?

Version=9.7;PriorCompany=NONE;Password=123abc;userName=JAZARETPC;testServer=ONLINE;
0
 
LVL 19

Accepted Solution

by:
NickUpson earned 40 total points
ID: 17885010
there is no quick & dirty way as the position & length of the part you want varies

if it was in the same place you could do it, you need something like

for select ... into ... :myvar
do
begin
  i = 0;
  vernum = 0;
  while (substr(myvar, i, 1) < '0' and substr(myvar, i, 1) > '9')
    vernum = (varnum * 10) + substr(myvar, i, 1)
  update mytable set vernum = :vernum where ...
end
0
 
LVL 3

Author Comment

by:yourbudweiser
ID: 17885106
thanks for the help, this is too comlicated for me as I cannot modify the database by adding stored procedures or additional fields in the table. Thanks for taking a look though.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question