CREATE TABLE #tb
(
id INT IDENTITY ,
LocationName VARCHAR(24) ,
LocationIndex INT NULL
)
INSERT INTO #tb
(
LocationName
)
SELECT '1015-02A01'
UNION
SELECT '1015-01A01'
UNION
SELECT '1016-04A01'
UNION
SELECT '1016-05A01'
UNION
SELECT '1017-01A01'
UNION
SELECT '1010-01A01'
UNION
SELECT '1016-02A01'
UNION
SELECT '1016-07A01'
UNION
SELECT '1015-03A01'
UNION
SELECT '1015-04A01'
SELECT * ,
new_id = row_number() OVER ( ORDER BY CASE WHEN LEFT(LocationName, 2) = 10
THEN LEFT(LocationName, 4)
END DESC
, CASE WHEN LEFT(LocationName,
4) IN ( '1015',
'1016' )
THEN SUBSTRING(LocationName,
6, 2) % 2
END, SUBSTRING(LocationName,
6, 2) )
FROM #tb WITH ( NOLOCK )