Question

I am writing:

Select datediff(hh,getutcdate(), End_Date) as "Duration" 
from MasterTable 
order by Duration

To get difference of hour values in ascending order. I get output,

Duration
--------
    -259
    -210
       5
      10
      22
      35
      75
     105
     235

But I want, ordering has to be done based on positive numbers and negative numbers of hour values. So, I need output as:

Duration
--------
       5
      10
      22
      35
      75
     105
     235
    -259
    -210

Is there any tweak to do such task? Thanks in advance.

Was it helpful?

Solution

If you do want the sequence, as you've indicated in the question, where it's all of the positive numbers in ascending order first, then the negative numbers in ascending order, it should be:

Select datediff(hh,getutcdate(),End_Date) as "Duration" from MasterTable
order by CASE WHEN Duration >= 0 THEN 0 ELSE 1 END,Duration

OTHER TIPS

Understanding that what you need is to ignore Duration sign:

Try using ABS:

Select datediff(hh,getutcdate(),End_Date) as "Duration" from MasterTable order by ABS(Duration)

Try this,

Select Abs(datediff(hh,getutcdate(),End_Date)) as "Duration" from MasterTable order by Abs(Duration)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top