Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

How to display products or records according to different type of sorting in sql server 2008?

Low To High Price
High To Low Price
Best Popular
Latest

These are sorting options.

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted

Low To High Price
SQL
SELECT * FROM MyTable ORDER BY Price

High To Low Price
SQL
SELECT * FROM MyTable ORDER BY Price DESC

Best Popular
SQL
SELECT * FROM MyTable ORDER BY OrderCount DESC

Latest
SQL
SELECT * FROM MyTable ORDER BY InsertDate DESC
 
Share this answer
 
you should make dynamic query in sql..

SQL
declare @strQuery nvarchar(max),@ColumName varchar(250),@Order varchar(10)

select @strQuery='SELECT * FROM MyTable ORDER BY '+ @ColumName +' ' + @Order
exec (@strQuery)


now if you want to sort data according to price than pass @ColumName='price' and @Order='asc' or 'Desc'

SQL
declare @strQuery nvarchar(max),@ColumName varchar(250),@Order varchar(10)

set @strQuery=' Price'
set @Order=' ASC'

select @strQuery='SELECT * FROM MyTable ORDER BY '+ @ColumName +' ' + @Order
exec (@strQuery)
 
Share this answer
 
You need columns and data associated with the options you want to sort on first (like price, popularity and create date, then you do a SQL order by : http://www.w3schools.com/sql/sql_orderby.asp[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900