Click here to Skip to main content
12,403,391 members (71,819 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL-Server MySQL
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 3-Oct-11 1:12am
Asutosha4.9K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
v2
Comments
CodeHelper_PS 3-Oct-11 7:26am
   
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
   
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.

Cheers!
  Permalink  
v2
Comments
Mehdi Gholam 3-Oct-11 7:35am
   
I don't think this will work.
Mario Majcica 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 Majcica 4-Oct-11 3:15am
   
Sometimes simple things can surprise! I love simplicity! ;)
Simple.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 Majcica 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.
Simple.v 5-Oct-11 0:42am
   
In Sql server 2005 I am not getting the names that starts with 'e'
Member 3918874 14-Sep-15 16:00pm
   
Thank you for you post.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Try as below Query.
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
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Try:
SELECT * FROM myTable
WHERE SUBSTRING([employeeName], 1, 1) >= 'A' AND SUBSTRING([employeeName], 1, 1) <= 'E'
(Note that SQL SUBSTRING is one based, not zero)
  Permalink  
Comments
Asutosha 3-Oct-11 8:04am
   
Not working in MYSQL.

Please can u provide me for MYSQL.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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.

Yogesh.
  Permalink  
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 4 Oct 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100