Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 2 case statements in order by help

Author  Topic 

Hommer
Aged Yak Warrior

808 Posts

Posted - 2013-10-31 : 10:54:55
Hey, everyone,

I could use a big help to get this working.

It will be part of the stored proc, but for now I couldn't even get it running in ssms.

It will be two parameters/variables, one for order by column name and other for order by direction, i.e. desc or asc.

I have tried following three ways, but none is working:
(1)
order by case when @Sort_by= '[A_ID]' AND @Sort_Dir ='Desc' then A_ID end desc
case when @Sort_by= '[A_ID]' AND @Sort_Dir ='Asc' then A_ID end asc
(2)
order by case when @Sort_by= '[A_ID]' AND @Sort_Dir ='Desc' then A_ID desc end
case when @Sort_by= '[A_ID]' AND @Sort_Dir ='Asc' then A_ID asc end
(3)
ORDER BY CASE @Sort_by when '[A_ID]' then [A_ID] end
Case @Sort_Dir when 'Desc' then desc end

Thanks!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-10-31 : 10:59:45
[code]
order by
case when @Sort_by = '[A_ID]' AND @Sort_Dir = 'Desc' then A_ID end desc,
case when @Sort_by = '[A_ID]' AND @Sort_Dir = 'Asc' then A_ID end asc
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2013-10-31 : 11:21:33
All I missed is a big , ?

Thanks!

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-10-31 : 22:24:14
yup


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -