I'm having some troubles sorting a table effectively (the table contains a big amount of rows so any optimization would make a big difference).
What I currently have that gives the correct result (11) is the code below.
BEGIN TRANSACTION
CREATE TABLE #tPallets
(
PalletNumber bigint,
Placement nvarchar(4)
)
INSERT INTO #tPallets VALUES(100000, 'B')
INSERT INTO #tPallets VALUES(100001, 'M1')
INSERT INTO #tPallets VALUES(100002, 'M2')
INSERT INTO #tPallets VALUES(100003, 'M3')
INSERT INTO #tPallets VALUES(100004, 'M4')
INSERT INTO #tPallets VALUES(100005, 'M5')
INSERT INTO #tPallets VALUES(100006, 'M6')
INSERT INTO #tPallets VALUES(100007, 'M7')
INSERT INTO #tPallets VALUES(100008, 'M8')
INSERT INTO #tPallets VALUES(100009, 'M9')
INSERT INTO #tPallets VALUES(100010, 'M10')
INSERT INTO #tPallets VALUES(100011, 'M11')
SELECT
TOP 1 CASE
WHEN Placement LIKE 'M%' THEN CONVERT(int, SUBSTRING(Placement,2, len(Placement)-1))
ELSE 0
END AS PALLET_PLACEMENT
FROM
#tPallets
ORDER BY
1 DESC
ROLLBACK TRANSACTION
So I am looking for a way to make the select faster if that's possible in this specific scenario.
I wouldn't consider this a duplicate in the way of answers. Since no answer in the thread would make the execution time faster without making the sort in C# (as opposed to doing it in TSql). And I have a hard time believing that there is no faster way to do it in TSql itself.