How do I create a dynamic SQL Server 2005 order by clause?

Hi,

I have a procedure that sorts faculty.  I need to print a report thta lists Admin first then regular instructors.  The database store the adminpositionID where 0 is a regular teacher and varios admin positions are > 0.  The Dean is 1, chair = 2.

My query spills out the faculty first since they are 0 but I want the admin to appear first sorted Asc (puts Dean first) then the faculty.  If I change my procedure to desc then the admin is first BUT the Dean appears last.

I need some sort of dynamic order by clause.  Something like Order By Case CVAdminPosionID > 0 Then Asc....
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 
ALTER PROC [dbo].[qGetCVInstructorsActiveByDepartmentID] 
@DepartmentID int
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
 
	BEGIN TRAN
	SELECT [pkInstructorID], [Active], CVAdminPositionID, [DateAdded], [DateUpdated], [StartYear], [YearsAdmin], [YearsArabLearners], [YearsCNAQ], YearsCNAQAdmin, [YearsESL], [YearsFSLFFL], [YearsPostSecondary], [YearsTotal],
	AdminPositionName, LastName, FirstName, InstructorID, InstructorNameIDCombo, DepartmentID, DepartmentName
	FROM   vGetCVInstructors
	Where (DepartmentID = @departmentid  OR @departmentid IS NULL OR @departmentid = 0 )
	And Active = 1
	Order By CVAdminPositionID Asc, DepartmentName, LastName, FirstName, InstructorID
		
	COMMIT

Open in new window

vstackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

momi_sabagCommented:
try this

SELECT [pkInstructorID], [Active], CVAdminPositionID, [DateAdded], [DateUpdated], [StartYear], [YearsAdmin], [YearsArabLearners], [YearsCNAQ], YearsCNAQAdmin, [YearsESL], [YearsFSLFFL], [YearsPostSecondary], [YearsTotal],
      AdminPositionName, LastName, FirstName, InstructorID, InstructorNameIDCombo, DepartmentID, DepartmentName
      FROM   vGetCVInstructors
      Where (DepartmentID = @departmentid  OR @departmentid IS NULL OR @departmentid = 0 )
      And Active = 1

      Order By case when adminpositionID > 0 then 0 else 1 end,
CVAdminPositionID Asc, DepartmentName, LastName, FirstName, InstructorID
            
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vstackAuthor Commented:
I am out of office now.  I'll check later and let you know.  I think it will work.
0
vstackAuthor Commented:
It worked great.  Although I don't completely understand how > 0 then 0 else 1 sorts correctly.  I would have thought all CVAdminpositionID's > 0 would be 1 and then spill out according to the rest of the order by clause?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.