Solved

Sorting Strings Numerically

Posted on 2006-11-06
6
652 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
Comment Utility
what version of firebird are you using?
0
 
LVL 3

Author Comment

by:yourbudweiser
Comment Utility
Hi NickUpson, we are using Firebird 1.5
0
 
LVL 19

Expert Comment

by:NickUpson
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Author Comment

by:yourbudweiser
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Read about achieving the basic levels of HRIS security in the workplace.
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now