Click here to Skip to main content
15,441,078 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.

Regards
Asutosha
Posted
Updated 25-Aug-17 2:21am

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


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


Good luck!
 
Share this answer
 
v2
Comments
P.Salini 3-Oct-11 7:26am     CRLF
My 5 :) In this situation like operator is best option
RaisKazi 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     CRLF
This above query is working in SQL Server, but not in MYSQL. can you give me the exact query for MYSQL ? Thanks
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?

SQL
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.

Cheers!
 
Share this answer
 
v2
Comments
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     CRLF
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     CRLF
I don't know MySql.I have a small doubt..Does this query returns the name that starts with 'e'. Example Eswar.
Try as below Query.
SQL
SELECT *
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.
http://msdn.microsoft.com/en-us/library/ms179859.aspx
http://msdn.microsoft.com/en-us/library/ms187489.aspx
 
Share this answer
 
Comments
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.
Try:
SQL
SELECT * FROM myTable
WHERE SUBSTRING([employeeName], 1, 1) >= 'A' AND SUBSTRING([employeeName], 1, 1) <= 'E'
(Note that SQL SUBSTRING is one based, not zero)
 
Share this answer
 
Comments
Asutosha 3-Oct-11 8:04am     CRLF
Not working in MYSQL. Please can u provide me for MYSQL.
You should try this.
SQL
SELECT Name FROM Employees WHERE Name LIKE '[A-E][F-J][K-O][P-T][U-Z]%'

I hop this will help you.

Yogesh.
 
Share this answer
 
v2
Comments
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