Click here to Skip to main content
15,392,472 members
Please Sign up or sign in to vote.
1.73/5 (4 votes)
See more:
Dear Sir,

I have a Application in which I have to display the name of Employee in range like .

A-E | F-J | K-O | P-T | U-Z

I want a SQL query to select those employee whose Names range between A to   E and so on.

When a user click on the link A-E , it should display all the employee name starting from A to E.

Thanks in advance.

Updated 25-Aug-17 2:21am

You could use LIKE for that. Try something like this query:
SELECT Name FROM Employees WHERE Name LIKE '[A-E]%'

SELECT Name FROM Employees WHERE Name REGEXP '^[A-E].*$'

Good luck!
P.Salini 3-Oct-11 7:26am
My 5 :)
In this situation like operator is best option
[no name] 3-Oct-11 7:26am
My 5!
Mehdi Gholam 3-Oct-11 7:27am
Never knew you could do that, my 5!
Asutosha 3-Oct-11 8:02am
This above query is working in SQL Server, but not in MYSQL. can you give me the exact query for MYSQL ?

E.F. Nijboer 4-Oct-11 12:20pm
With MySQL you can use REGEX (or RLIKE) to do that. I updated the answer with a MySQL query that does the same.
What is wrong with a simple BETWEEN?

SELECT name FROM test WHERE name BETWEEN 'a' AND 'e'

Where name is column name and test is table name.

This is even easy to parametrise for an eventual SP.

Mehdi Gholam 3-Oct-11 7:35am
I don't think this will work.
Mario Majčica 3-Oct-11 7:54am
Try it! ;)
Mehdi Gholam 4-Oct-11 3:12am
I stand corrected!
Very interesting indeed I never knew that!
my 5!
Mario Majčica 4-Oct-11 3:15am
Sometimes simple things can surprise! I love simplicity! ;)
sravani.v 4-Oct-11 8:31am
I don't know MySql.I have a small doubt..Does this query returns the name that starts with 'e'.
Example Eswar.
Mario Majčica 4-Oct-11 8:49am
I didn't tested it on MySQL, but MySQL was specified only after I posted the answer. On MS SQL is working.
sravani.v 5-Oct-11 0:42am
In Sql server 2005 I am not getting the names that starts with 'e'
Angel2015 14-Sep-15 16:00pm
Thank you for you post.
Try as below Query.
FROM Employee
WHERE Name LIKE '[A-E]%';

You may write Stored Procedure which will accept range and then change above query based on parameter values of Stored Procedure.

Have a look at below links for more information.
Asutosha 3-Oct-11 8:02am
This above query is working in SQL Server, but not in MYSQL. can you give me the exact query for MYSQL ?
Rohini Palanichamy 21-Nov-12 2:15am
Thank Raiskazi .I got my query from your link.
WHERE SUBSTRING([employeeName], 1, 1) >= 'A' AND SUBSTRING([employeeName], 1, 1) <= 'E'
(Note that SQL SUBSTRING is one based, not zero)
Asutosha 3-Oct-11 8:04am
Not working in MYSQL.

Please can u provide me for MYSQL.
You should try this.
SELECT Name FROM Employees WHERE Name LIKE '[A-E][F-J][K-O][P-T][U-Z]%'

I hop this will help you.

André Kraak 4-Oct-11 5:52am
DO NOT SHOUT. When you use all capital letters it is seen as shouting on the Internet and considered rude. Next time when posting please use proper capitalization.
Member 13025266 7-Jul-17 0:11am
If I want Employee Name start with only capital 'A' then what to do in SQL?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900