Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can i sort the record (alphabetically) in table by excluding specific row and that excluded row has to be displayed first by default.

for example

Let my table be as follow

Id Name
1 A
2 B
3 c
4 x

i need the record to be sorted as follow

Name
X
A
B
C
Posted
Comments
__TR__ 28-Nov-12 4:57am    
On what basis are you excluding specific rows ?
n.sriganesh 28-Nov-12 7:59am    
I want to display x row as initial record so i need to exclude

SELECT Name FROM Table WHERE Name=X
UNION
SELECT NAME FROM Table WHERE Name <> X ORDER BY Name

Actually, you have two tables one after another
 
Share this answer
 
Comments
n.sriganesh 28-Nov-12 8:00am    
Your query worked fine thanks for your help
Here is a sample approach
SQL
CREATE TABLE #SampleTable
(
    ID INT,
    [Name] VARCHAR(50)
)

INSERT INTO #SampleTable
SELECT 1, 'A' UNION ALL
SELECT 2, 'B' UNION ALL
SELECT 3, 'C' UNION ALL
SELECT 4, 'X'

SELECT [Name] FROM
(
    SELECT CASE WHEN ID = 4 THEN 0 ELSE 1 END AS SortOrder, ID, [Name] FROM #SampleTable
) T
ORDER BY SortOrder, [Name]

DROP TABLE #SampleTable
 
Share this answer
 
Comments
n.sriganesh 28-Nov-12 8:00am    
May i know what is that T denote i am new to it
__TR__ 28-Nov-12 8:34am    
T is the Alias name for the query (SELECT CASE WHEN ID = 4 THEN 0 ELSE 1 END AS SortOrder, ID, [Name] FROM #SampleTable)

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