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 a character column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-12-09, 16:14
Rkemper58 Rkemper58 is offline
Registered User
 
Join Date: Oct 2009
Posts: 4
sorting a character column

I am trying to sort a column defined as char length of 10.
Values are right adjusted. Example:
' 050120'
' 050149'
' 050250'
' 060100'
' 050149R'
I would like the values 050149 & 050149R sorted together, instead of having the 050149R show up at the end. I saw a similar post but haven't been able to accomplish the task. Would appreciate any help.
Reply With Quote
  #2 (permalink)  
Old 10-12-09, 16:24
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
How would you like to see sorted table ?
Reply With Quote
  #3 (permalink)  
Old 10-12-09, 16:26
Rkemper58 Rkemper58 is offline
Registered User
 
Join Date: Oct 2009
Posts: 4
For the example given, this would be the sorted results:
' 050120'
' 050149'
' 050149R'
' 050250'
' 060100'
Reply With Quote
  #4 (permalink)  
Old 10-12-09, 16:31
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
For example:

Quote:
select charcol, t1.*
from table1 t1
order by left(charcol, 6)
Lenny
Reply With Quote
  #5 (permalink)  
Old 10-12-09, 16:48
Rkemper58 Rkemper58 is offline
Registered User
 
Join Date: Oct 2009
Posts: 4
I just tried that LEFT scalar function and the values with an Alpha on the end are still grouped at the end of the sort. It must be because the values that have a trailing alpha begin in position 4 of the field and those that don't have the alpha character begin in position 5 of the field.
Reply With Quote
  #6 (permalink)  
Old 10-12-09, 17:11
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
So, you can try:

Quote:
select charcol, t1.*
from table1 t1
order by substr(charcol, 1, 6), charcol
Lenny
Reply With Quote
  #7 (permalink)  
Old 10-12-09, 17:11
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
String comparison goes throw 2 strings character-by-character and if the character in the first string comes before the corresponding character in the other string, the first string is lexicographically sorted before the other string. In your case, you reach the point where a space is compared with '0', which leads to all strings with a space in that position to be sorted before all strings with a '0'. What you have to do is get rid of the leading spaces. A function like LTRIM() may be what you need, e.g. ORDER BY LTRIM(myCol).

Code:
$ db2 "select * from table ( values ('  050120'),('  050149'),('  050250'),('  060100'),(' 050149R') ) as t(c) order by c"

C
--------
  050120
  050149
  050250
  060100
 050149R

  5 record(s) selected.

$ db2 "select * from table ( values ('  050120'),('  050149'),('  050250'),('  060100'),(' 050149R') ) as t(c) order by ltrim(c)"

C
--------
  050120
  050149
 050149R
  050250
  060100

  5 record(s) selected.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 10-12-09, 17:24
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Oh, I did not see space in position1:

Quote:
select charcol, t1.*
from table1 t1
order by left(charcol, 7)
or

Quote:
select charcol, t1.*
from table1 t1
order by substr(charcol, 2, 6), charcol
Lenny
Reply With Quote
  #9 (permalink)  
Old 10-13-09, 01:57
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
@Lenny77:

The values are right adjusted. There are different number of preceeding blancs. Therefore SUBSTR() or LEFT() don't work.

As stolze mentioned, LTRIM() [ ... or STRIP() ] is the solution
Reply With Quote
  #10 (permalink)  
Old 10-13-09, 09:16
Rkemper58 Rkemper58 is offline
Registered User
 
Join Date: Oct 2009
Posts: 4
Thanks one and all for the valuable assistance.
It sorts perfectly by using
ORDER By
LTRIM(Charcol)
Reply With Quote
  #11 (permalink)  
Old 10-13-09, 10:01
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs down

Quote:
Originally Posted by umayer
@Lenny77:

The values are right adjusted. There are different number of preceeding blancs. Therefore SUBSTR() or LEFT() don't work.

As stolze mentioned, LTRIM() [ ... or STRIP() ] is the solution
Oh, boy !
You are right. I did not read about right adjusting !

Lenny
Reply With Quote
  #12 (permalink)  
Old 10-13-09, 13:18
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Bad formatting in the original post, I guess. Without right-adjustment, you don't need to do anything - just sort the left-adjusted values and that's it.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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