user3795898
user3795898

Reputation: 11

SQL Server Order By

I need to order my returned dataset by first the specified value and then by ascending order. For example:

Declare @Value varchar(55)
Set @Value = 'abc'

Select ID, Value
From Table1
Order by ID ASC

I want to make sure that first my 'abc' value prints out on the first row, then the rest of the dataset should be sorted by ID in ascending order. Is this possible? Thanks

Upvotes: 0

Views: 37

Answers (1)

cha
cha

Reputation: 10411

yes. If your IDs start with 1, 2, 3.... etc. you can use this query:

Select ID, Value
From Table1
Order by CASE WHEN value = @value then 0 else ID END ASC

Upvotes: 1

Related Questions