You can do a lot in an SQL query. But sometimes, you should probably do less, like this ad-hoc ORDER BY clause from KT.

sql = sql + "Order By Section, " +"replace(replace(replace(replace(" +
    "replace(replace(replace(replace(replace(replace(replace(replace" +
"(replace('.' +Num + '.','.2.','.02.'),'.1.','.01.'),'.3.','.03.')" +
        ",'.4.','.04.'),'.6.','.06.'),'.5.','.05.'),'.7.','.07.')" + 
        ",'.8.','.08.'),'.9.','.09.'),'.5.','.05.'),'.1.','.01.')" +
        ",'.2','.02'),'.3','.03')";

The line-breaks were not in the original, but I wanted this to be vaguely readable.

On one hand, this is possibly the symptom of a really bad data model, where Num fields encode some meaningful identifier one way, but other applications need it represented another way. But then I notice that some of the replacements get repeated (.5. and .1.), and some are just similar (.2. vs. .2), and then I suspect someone just had no clue what was happening here.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!