yourbudweiser
asked on
Sorting Strings Numerically
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!
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!
what version of firebird are you using?
ASKER
Hi NickUpson, we are using Firebird 1.5
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
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
ASKER
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=N ONE;Passwo rd=123abc; userName=J AZARETPC;t estServer= ONLINE;
Version=9.7;PriorCompany=N
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.