I am using SQL Server 2005.
Please consider the following query:
DECLARE @SearchTerm NVARCHAR(4)
SELECT @SearchTerm = 'X'
SELECT ProductCode -- NVARCHAR type
FROM Product
WHERE ProductCode LIKE '%' + @SearchTerm + '%'
How may I specify the correct grouping / order by clause to my query to order the results by the following specific criteria?
- Firstly, order by ProductCode ascending where ProductCode STARTS with @SearchTerm
- Then order the remaining results by ProductCode ascending.
So for example the results may look like:
ProductCode
-----------
XAA
XMA
XZA
AXA
AXZ
BAX
BMX
BXZ
etc.
Any answers as ever are appreciated.
Thanks everyone for the answers however I have come up with my preferred solution myself based off noting the syntax other answers have used
LIKE @SearchTerm + '%', i.e. dropping the first'%'gave me the clue which reminded me of an old Excel trick: