You can use CHARINDEX an odd way: look for the identifier in a comma-separated list and sort the result by position,
Consider this list, for example, 10,9,5,7,3,8 ... substring 10 appears at position 1 st and 9 appears at 4 th . Just order a substring position.
CREATE TABLE Emp (EmpId int, EmpName varchar(100), Sal int) ; INSERT INTO Emp (EmpId, EmpName, Sal) VALUES (1, 'John', NULL), (2, 'Jane', NULL), (3, 'Smith', NULL), (4, 'Doe', NULL), (5, 'Ben', NULL), (6, 'Steve', NULL), (7, 'Andrew', NULL), (8, 'Simon', NULL), (9, 'Jack', NULL), (10, 'Allen', NULL) ; SELECT EmpId, EmpName, Sal FROM Emp WHERE EmpId in (10,9,5,7,3,8) ORDER BY CHARINDEX(CONCAT(',', EmpId, ','), CONCAT(',', '10,9,5,7,3,8', ',')) ;
Result:
EmpId | EmpName | Sal ------+---------+----- 10 | Allen | NULL 9 | Jack | NULL 5 | Ben | NULL 7 | Andrew | NULL 3 | Smith | NULL 8 | Simon | NULL