Question : order by (-) number

How is that a order by negative number is possible?

select * from sys.indexes order by case type_desc when 'clustered' then -1 else type end desc

thanks

Answer : order by (-) number

order by clause will not accept the negative number.

if you are trying to give

select * from sys.indexes order by -1 desc

it wont accept, it will give you the following error

Msg 108, Level 16, State 1, Line 1
The ORDER BY position number -1 is out of range of the number of items in the select list.

Here you are ordering by the type desc column and the type_desc column contains the negative values. So It a negative data. In that case it will accept that.

Check the following query, you will get a clear picture.

select case type_desc when 'clustered' then -1 else type end, * from sys.indexes
order by case type_desc when 'clustered' then -1 else type end desc

here see the first column, you can understand how the sql server engine has sorted.
Random Solutions  
 
programming4us programming4us