×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

SORT by part of filed
2

SORT by part of filed

SORT by part of filed

(OP)

CODE

SELECT DISTINCT LISTINO_BAR.REPARTO FROM REPARTI INNER JOIN LISTINO_BAR ON REPARTI.IDREP = Val(LISTINO_BAR.REPARTO) ORDER BY LISTINO_BAR.REPARTO 


but i need to sort from only a numeric value and not to the all in field LISTINO_BAR.REPARTO

note:
the numeric value are dinamic, i can have:

987-....
12-...
0-....

ecc

RE: SORT by part of filed

Hi sal21,

You have not given a specific example, so I tried this data

CODE

create or replace table mytab (
  rec character(20)
)
;

insert into mytab 
values
('0555-baz'),
('001-foo'),
('12-bar'),
('9999-foobar')
;

select * from mytab
; 

So I have now the table MYTAB with one column REC, which contains the following data

CODE

REC
0555-baz            
001-foo             
12-bar              
9999-foobar 

Now, how do I sort the table by the numeric part of the record?
Using CTE (=Common Table Expressions) I create a temporary table with additional column NUMBER, where I extract the numeric string part from the column REC using regular expression function REGEXP_SUBSTR() and convert it into integer number using CAST() , i.e.:

CODE

with mytab_with_number(rec, number) as (
  select
    rec,
    cast(REGEXP_SUBSTR(rec, '\d+', 1, 1) as int) as number
  from mytab
)
select * from mytab_with_number
; 

The result I got is this temporary table with the original column REC and additional column NUMBER:

CODE

REC                     NUMBER
0555-baz            	555
001-foo             	1
12-bar              	12
9999-foobar         	9999 

Finally, I can sort that table by the NUMBER column:

CODE

with mytab_with_number(rec, number) as (
  select
    rec,
    cast(REGEXP_SUBSTR(rec, '\d+', 1, 1) as int) as number
  from mytab
)
select * from mytab_with_number
order by number
; 
and I get the desired result

CODE

REC                     NUMBER
001-foo             	1
12-bar              	12
0555-baz            	555
9999-foobar         	9999 

RE: SORT by part of filed

tkx micton.
but i cannot test now.
is for Access database?

RE: SORT by part of filed

No it's not for MS Access database, I tested it on IBM DB2, I don't have MS Access and don't know if the function REGEXP_SUBSTR() or similar is available on MS Access.

RE: SORT by part of filed

If you are sure there will always be a numeric followed by a hyphen, the following should work. I don't have DB2 so I can't test this.

CODE

CODE
SELECT DISTINCT LISTINO_BAR.REPARTO 
  FROM REPARTI 
       INNER JOIN LISTINO_BAR 
          ON REPARTI.IDREP = Val(LISTINO_BAR.REPARTO) 
ORDER BY SUBSTR(LISTINO_BAR.REPARTO ,INSTR(LISTING_BSR.REPARTO,'-')+1); 

RE: SORT by part of filed

tks micron and carp

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close