Click here to Skip to main content
Rate this: bad
good
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 2:12am
Asutosha4.3K
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 at 3-Oct-11 7:26am
   
My 5 :)
In this situation like operator is best option
RaisKazi at 3-Oct-11 7:26am
   
My 5!
Mehdi Gholam at 3-Oct-11 7:27am
   
Never knew you could do that, my 5!
Asutosha at 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 at 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 at 3-Oct-11 7:35am
   
I don't think this will work.
Mario Majcica at 3-Oct-11 7:54am
   
Try it! ;)
Mehdi Gholam at 4-Oct-11 3:12am
   
I stand corrected!
Very interesting indeed I never knew that!
my 5!
Mario Majcica at 4-Oct-11 3:15am
   
Sometimes simple things can surprise! I love simplicity! ;)
Simple.v at 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 at 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 at 5-Oct-11 0:42am
   
In Sql server 2005 I am not getting the names that starts with 'e'
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 at 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 at 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 at 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 at 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
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 4 Oct 2011
Copyright © CodeProject, 1999-2014
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