sql server - Conditional SQL ORDER BY ASC/DESC for alpha columns -


writing stored procedure in ms sql server 2008 r2, want avoid using dsql...

i sort method (asc or desc) conditional.

now, numeric column use case statement , negate value emulate asc or desc... is:

... order case @orderascordesc when 0 [numericcolumn] else -[numericcolumn] end asc 

what appropriate method doing alpha column?

edit: thought of clever way seems terribly inefficient... insert ordered alpha column temp table autonumber sort autonumber using method described above.

edit2:

what guys think of approach?

order case @orderascordesc when 0 [alphacolumn] else '' end asc, case @orderascordesc when 0 '' else [alphacolumn] end desc 

i don't know if forcing sort on uniform column more efficient deriving numbers sorted strings though

one option

;with cquery (    select        *,        row_number() on (order sortcolumn) rownum           mytable ) select    *    cquery order    rownum * @direction --1 = asc or -1 = desc 

or case imho bit uglier

order   case when 'asc' sortcolumn else '' end asc,   case when 'desc' sortcolumn else '' end desc 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -