Quantcast
Channel: CodeFari
Viewing all articles
Browse latest Browse all 265

Ordering SQL query by specific field values in SQL Server

$
0
0
I am doing custom sorting in order by clause in SQL Server. As we know ORDER BY clause use to sort results returned by SELECT statement. If we apply ORDER BY clause on particular column which is VARCHAR data type it will sort this according dictionary. but if we want to sort this column from particular value the we can customize ORDER BY clause.
See bellow example.


CREATETABLE#TMP
(
       IDINTIDENTITY(1,1),
       NAMEVARCHAR(100),
)

INSERTINTO#TMP
SELECT'ASHISH'
UNIONALL
SELECT'CHANDAN'
UNIONALL
SELECT'DILIP'
UNIONALL
SELECT'ESHA'
UNIONALL
SELECT'FIZA'
UNIONALL
SELECT'MAHESH'
UNIONALL
SELECT'VIPUL'
UNIONALL
SELECT'ANIL'

-- I want to sort NAME column from value 'DILIP' then query will be as bellow

SELECT*FROM#TMPORDERBYCASEWHENNAME='DILIP'THEN'1'ELSENAMEENDASC

DROPTABLE#TMP





Viewing all articles
Browse latest Browse all 265

Trending Articles