Solved

Sorting Strings Numerically

Posted on 2006-11-06
6
656 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS Loading Excel file to SQL database 10 64
Need a mirrored QA test site 2 95
Automate an Oracle update in Excel 7 70
RMAN-06100 when restoring backupset 4 12
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…

752 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