Link to home
Start Free TrialLog in
Avatar of barnesco
barnesco

asked on

Dynamic Order By and ASC DESC

Referencing: stackoverflow.com/questions/1147763/dynamic-order-direction

I attempted to set mine up the same way, but I'm receiving the error message: Conversion failed when converting the varchar value 'StartDate' to data type int.

The below is what I have:

@OrderBy varchar(10) = 'StartDate',
@Direction int = 1

...
      CASE WHEN @Direction = 0 THEN 1
      ELSE
            CASE WHEN CAST(@OrderBy AS varchar(10)) = 'StartDate' THEN 'StartDate'
                  WHEN CAST(@OrderBy AS varchar(10))  = 'EndDate' THEN 'EndDate'
            END
      END ASC,
      
      CASE WHEN @Direction = 1THEN 1
      ELSE
            CASE WHEN CAST(@OrderBy AS varchar(10)) = 'StartDate' THEN 'StartDate'
                  WHEN CAST(@OrderBy AS varchar(10))  = 'EndDate' THEN 'EndDate'
            END
      END DESC
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of barnesco
barnesco

ASKER

Dang, that does work...thanks, AngelIII!!!