SQL Server | Other
Additional SQL Server features and topics not covered by specific categories
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello!
I know that order by can be used for sorting, and adding desc can be used to sort in descending order.
But now I need to move some of the last values to the middle, how do I do that?
Additional SQL Server features and topics not covered by specific categories
Answer accepted by question author
Hi @15431565
You can use the case when statement in the order by.
Just like this.
create table test(col int);
insert into test values(4),(20),(36)
select * from test order by case when col = 4 then 1
when col = 20 then 3
when col = 36 then 2 end;
Output:
Best regards,
Percy Tang