Click here to Skip to main content
14,266,113 members
Rate this:
Please Sign up or sign in to vote.
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 .
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try this:
select * from tablename where status = 6
union
select * from tablename where status <> 6
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 2

You can use a custom sort by clause in your query.

For example
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
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100