Brij
Brij

Reputation: 13151

OrderBy in SQL Server to put positive values before negative values

I have table in SQL Server which contains a column of type "int". The column can contain positive as well as negative values. I want to carry out sorting based on this column values such that rows with positive values in this column come before the negative values.

Example:

Code SortColumn
A     1
B     5
C    -1
D    -3
E     0
F     2

Desired Output:

Code SortColumn
E        0
A        1
F        2
B        5
C       -3
D       -1

Upvotes: 9

Views: 5626

Answers (1)

bummi
bummi

Reputation: 27385

Select * from Table
order by 
Case when sortcolumn<0 then 1 else 0 end
,sortcolumn

Upvotes: 21

Related Questions