MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Different order sql for character data

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
dohellwithmf
Beginner


Joined: 12 Jul 2007
Posts: 55
Topics: 23

PostPosted: Wed Aug 01, 2007 1:38 am    Post subject: Different order sql for character data Reply with quote

i have a field data type as char(10) in a table. It has values as follows:

E1
1
2
E6
E2
E10
E11

The requirement is to fetch the values in the following order:
1, 2, E1, E2, E6, E10, E11

Please guide......

Question
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Wed Aug 01, 2007 4:37 am    Post subject: Student Problem Reply with quote

I assume that this is a student problem. I solved it with CASE, will only work for the given type of input. Will only work for Num-Space (numbers 0-9), and Alpha-num-num. Will not work for numbers > 9 or Alpha-num-alpha. Nor an entry greater than 3 chars.

change all 'TEXT' to <your column name>
change '<table>' to <your table name>
Code:

SELECT TEXT
     , CASE 
            WHEN SUBSTR(TEXT,1,1) BETWEEN '0' AND '9' THEN CONCAT('0',SUBSTR(TEXT,1,1))
            ELSE CONCAT('1',SUBSTR(TEXT,1,1))
        END AS SORT_KEY1
     , CASE 
            WHEN SUBSTR(TEXT,3,1) BETWEEN '0' AND '9' THEN CONCAT('0',SUBSTR(TEXT,3,1))
            WHEN SUBSTR(TEXT,3,1) BETWEEN 'A' AND 'Z' THEN CONCAT('1',SUBSTR(TEXT,3,1))
            ELSE ' '
        END AS SORT_KEY2
     , CASE 
            WHEN SUBSTR(TEXT,2,1) BETWEEN '0' AND '9' THEN CONCAT('0',SUBSTR(TEXT,2,1))
            WHEN SUBSTR(TEXT,2,1) BETWEEN 'A' AND 'Z' THEN CONCAT('1',SUBSTR(TEXT,2,1))
            ELSE ' '
        END AS SORT_KEY3
FROM <table>
ORDER BY SORT_KEY1
       , SORT_KEY2
       , SORT_KEY3;

_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
dohellwithmf
Beginner


Joined: 12 Jul 2007
Posts: 55
Topics: 23

PostPosted: Wed Aug 01, 2007 6:31 am    Post subject: Reply with quote

thnks Dick.... Laughing
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group