If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > sorting in asc but alpha's should come before numerics

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-04-11, 03:31
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
sorting in asc but alpha's should come before numerics

Hi All
Please check with the following query. I need the solution in this very soon .

Query :
select case when length(c_fam_extndd) > 3 then c_fam_extndd else c_fam_extndd|| ' ' ||x_fam_extndd end as fam from fcfm_npi.vpm_tc_trnsn order by fam with ur

Problem we are facing:
We want sort should be like alphabets first then digits in asc.
By sort we are getting following list of vals which should be sorted like alpha’s first then digits but in asc.

please reply me soon
Reply With Quote
  #2 (permalink)  
Old 02-04-11, 05:50
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You could create your database with a manually defined collation sequence (an example is described here: Case-insensitive string comparisons with DB2 for Linux, UNIX, and Windows). Alternatively, you could create a UDF that shuffles the code points in the desired way, i.e. iterate of the bytes/characters in the string and replace each byte in such a way that the desired ordering is accomplished. Then you can do:
Code:
SELECT ...
FROM ...
ORDER BY sort_udf(c_fam_extndd)
If your string contains only digits or only letters (but no mixture), you could use a CASE expression working on the first character. That would introduce a more significant sort criteria, i.e. letters map to 0 and digits to 1, implying that digits are sorted after letters.
Code:
SELECT ...
FROM ...
ORDER BY CASE SUBSTR(c_fam_extndd, 1, 1) BETWEEN '0' AND '9' THEN 1 ELSE 0 END, c_fam_extndd
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 02-04-11, 06:24
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
If your string contains only alphabets('a'-'z' and 'A'-'Z'), numeric digits('0'-'9') and blanks(' '),
here are another examples.

1) If you want to change collating sequence...
Code:
...
 ORDER BY
       TRANSLATE( fam
                , '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
                , 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789'
                ) ASC
2) If you want position all numeric strings after alphabet strings or mixed strings...
Code:
...
 ORDER BY
       ISNUMERIC(fam) ASC
     , fam ASC
Where ISNUMERIC user-defined-function is in
Sample UDFs for Migration

Last edited by tonkuma; 02-06-11 at 10:23. Reason: Remove " , fam" from example 1).
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On