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

I have a table which consists of 6 fields with one field name as status.
table consists of 10 rows.
Status field consists of data from 1 to 10.

Here I need to fetch the data based(order by) on the status value, but value may be varied depending on the requirement.

For example, record with status value 6 need to be fetched as a first row of the table

I tried in google but couldn't get it.

Please help me on this.

thanks in advance.
Posted
Comments
King Fisher 4-Mar-15 5:09am    
show your table structure and expected Resultset .

Try this:
SQL
select * from tablename where status = 6
union
select * from tablename where status <> 6
 
Share this answer
 
Comments
sri4dotnet 4-Mar-15 5:39am    
Thanks for your reply.
I have tried this but may be a case 6 cannot be in the table then the row will be null
Peter Leow 4-Mar-15 6:02am    
I do not get you. If the status of 6 does not exist, then there is no question of it being in the first row in the first place.
You can use a custom sort by clause in your query.

For example
SQL
SELECT Column1, Column2
                FROM   dbo.Country
                ORDER BY CASE WHEN Status = 6 THEN 1
                              WHEN Status = 5 THEN 2
                              ELSE Status END ASC

For more detailed information please check this link[^].

Good luck,
OI
 
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