Solved

Sorting Strings Numerically

Posted on 2006-11-06
6
658 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

632 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