sql server - T-SQL IF statements in ORDER BY clauses -


is there way can like

@sorttype  select     *     table order     -- if sorttype == id     table.id     -- else if sorttype == date     table.date 

how syntax look? thanks.

there's gotcha here... can't mix data types in case statement, need create different groups of case statements different data types.

select     *     table order     case when @sorttype = id table.id end asc,     case when @sorttype != id table.date end asc 

related blog post:

http://dirk.net/2006/11/14/dynamic-order-by-with-case-statement-in-sql-server-data-type-issues/


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -