I want a preface, I did not write this. I inherited this earlier today, and I struggled to find a solution. The following code base works - except that the price per square foot is returned in alphabetical order, not in numbers.
SELECT CTE.Suite, CTE.Building, CS.Address, CS.City, CS.State, CTE.Tenant, CTE.AverageCharges, CTE.SQFT, CTE.CostSQFT, CTE.TenantType FROM cteInvoices as CTE ... ORDER BY CASE @Order WHEN 'Suite' THEN CTE.Suite WHEN 'Cost' THEN cast(CTE.costSQFT as varchar) END
I changed the end of the case to the following:
ORDER BY CASE @Order WHEN 'Suite' THEN CTE.Suite WHEN 'Cost' THEN CTE.costSQFT END
This fixed the sorting of costs, however, when I choose to sort by Suite, I get:
Msg 8114, Level 16, State 5, Procedure rCostSQFTcsort, Line 17 Error converting data type varchar to numeric.
I am working on this evening, and nothing that I seem to be correcting is if something tends to make the situation worse. I hope that a second set of eyes can shed light on the problem.
source share