0

I want to display page number in ascending order. But, since the field PAGE is of String datatype, normal 'ORDER BY' considers 10 < 2. I have to make the field PAGE as String because there can be inputs like '3-4'. Can anyone please suggest a way out. I've attached screenshot for reference.

Kindly help.Screenshot

select id
    ,F_NL
    ,page
    ,title
from newsletter_content
where F_NL = '29'
order by page asc;
7
  • Could you give us what did you try so far? Commented Mar 23, 2017 at 12:00
  • Hi Andrew, I'm actually out of ideas. I thought of changing the Java code but it will be better if I can display it in the correct way using queries. Commented Mar 23, 2017 at 12:03
  • Sample data and desired results would really help. Commented Mar 23, 2017 at 12:06
  • So how do you expect to sort a "page number" of 3-4??? Commented Mar 23, 2017 at 12:15
  • Are there any other special page numbers (e.g. 'A', 'page 12', '1+2', '3f', '4ff', '1.5', '1,2,3')? Or only numbers ('1', '2', ...) and number ranges ('1-2', '3-5', ...)? Commented Mar 23, 2017 at 12:29

2 Answers 2

1
select page from p 
  order by to_number(nvl(substr(page, 1, instr(page, '-')-1), page))

rextester demo

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks a lot! :)
0

You can check for the presence of a - character and extract the preceding number:

ORDER BY CASE
           WHEN INSTR( page, '-' ) > 0
           THEN TO_NUMBER( SUBSTR( page, 1, INSTR( page, '-' ) - 1 ) )
           ELSE TO_NUMBER( page )
         END;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.