Q
Problem solve Get help with specific problems with your technologies, process and projects.

Sorting substrings of different lengths numerically, part 2

Here is the rest of Rudy's answer. See part one.

Anyhow, this detailed explanation could go on for a lot longer, so I'll just jump ahead to the end and give you the full query --

 SELECT IPno , RIGHT('00'& MID(IPno ,1 ,INSTR(IPno,'.')-1) , 3 ) , RIGHT('00'& MID(IPno ,INSTR(IPno,'.')+1 ,INSTR(MID(IPno ,INSTR(IPno,'.')+1) ,'.')-1) , 3 ) , RIGHT('00'& MID(IPno ,INSTR(MID(IPno ,INSTR(IPno,'.')+1) ,'.') +INSTR(IPno,'.')+1 ,INSTR(MID(IPno ,INSTR(MID(IPno ,INSTR(IPno,'.')+1) ,'.') +INSTR(IPno,'.')+1) ,'.')-1) , 3 ) , RIGHT('00'& MID(IPno ,INSTR(MID(IPno ,INSTR(MID(IPno ,INSTR(IPno,'.')+1) ,'.') +INSTR(IPno,'.')+1) ,'.') +INSTR(MID(IPno ,INSTR(IPno,'.')+1) ,'.') +INSTR(IPno,'.')+1) , 3 ) FROM IPtable ORDER BY 2, 3, 4, 5;

Ugly, isn't it? What a great candidate for a view!

 CREATE VIEW IPnumbers (IPno, IP1, IP2, IP3, IP4) AS  the SELECT statement above, minus the ORDER BY clause

Then you could say

 SELECT IPno, IP1, IP2, IP3, IP4 FROM IPnumbers ORDER BY IP1, IP2, IP3, IP4


This was last published in June 2002

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close