Subject SOLUTION TO - ORDER BY as number when not
Author Todd Brasseur
Here is the solution I came up with in trying to fix my problem with
ordering a string field as if it was a number.

I decided to use triggers as suggested by Paul Vinkenoog. I created a
new column in my table called QUICKNUM as DOUBLE PRECISION to store the
numerical equivalent of the QUICKKEY CHAR(7). But I couldn't just
test the cast and then set Quicknum to null if failed or I would get
results like:

Quickkey QuickNum
1 1
2 2
3 3
2A NULL

When I process the statement:
Select quickkey, quicknum From Codes Order by QuickNum, Quickkey

Because the 2A would have failed the test and been set to NULL.

So I created a Procedure (QUICKKEY_TO_QUICKNUM) that checks each of
the seven characters in the QUICKKEY to see if it is either a '.' or
if it fails the CAST to INTEGER test. Then I populate QUICKNUM in the
triggers by using:

SELECT QuickNum From QUICKKEY_TO_QUICKNUM(NEW.Quickkey) Into :QuickNum;

So now I get:

Quickkey QuickNum
1 1
2 2
2A 2
3 3

When I process the statement:
Select quickkey, quicknum From Codes Order by QuickNum, Quickkey

Now I just have to change all the 'order by' s in the application.

If you would like a copy of the metadata for the Stored Procedure, just
let me know.

Todd Brasseur
COMPASS Municipal Services Inc.