SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 2 case statements in order by help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Hommer
Aged Yak Warrior

786 Posts

Posted - 10/31/2013 :  10:54:55  Show Profile  Reply with Quote
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)

Singapore
17599 Posts

Posted - 10/31/2013 :  10:59:45  Show Profile  Reply with Quote

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



KH
Time is always against us

Go to Top of Page

Hommer
Aged Yak Warrior

786 Posts

Posted - 10/31/2013 :  11:21:33  Show Profile  Reply with Quote
All I missed is a big , ?

Thanks!

Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17599 Posts

Posted - 10/31/2013 :  22:24:14  Show Profile  Reply with Quote
yup


KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000