Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query, where name in column begins with a certain letter; clarification

I have a vb.net application running a process. In order to speed it up I have split it into several threads, the process asks for employees in groups of names as below.

 SELECT * FROM Employ WHERE Name like '[A-C]%' ORDER BY RDate DESC

Mostly I have groups like 'A-C' and so on, but it just so happens that the 'Ms' are the biggest group, so I made them their own group, 'M-M' and even that is not enough, their thread takes way too long to finish. So I would like to make 2 groups like this 'Ma-Mm' and 'Mn-Mz'.

Is it just a case of changing the above query to those letters? like:

SELECT * FROM Employ WHERE Name like '[Ma-Mm]%' ORDER BY RDate DESC
SELECT * FROM Employ WHERE Name like '[Mn-Mz]%' ORDER BY RDate DESC

Also providing that that name column is indexed, is this the most optimal way to write that query?

Edit: I am using Ms SQL Server

like image 877
user1500403 Avatar asked Jan 27 '26 10:01

user1500403


1 Answers

If you are using SQL Server, you can do:

WHERE Name like 'M[a-m]%' 

However, a generate range, such as My-No, would be harder to express. You can always do:

WHERE name >= 'Ma' and name < 'Mn'

as well. This works for more complex ranges.

like image 159
Gordon Linoff Avatar answered Jan 30 '26 02:01

Gordon Linoff