views:

16

answers:

1

I have a set of students at a high school. The counselors want to divide the students up by last name. Here is the break down:

Counselor 1: A to F Counselor 2: G to Hr Counselor 3: Hs to O Counselor 4: P - Z

The first one is easy, I just do a:

where last_name like '[A-F]%'

but the second counselor is giving me grief, because if I do:

where last_name like '[G-Hr]%'

...I get all students with last names of G, H, and R. What is the best way to get what I want for counselor's 2 and 3?

+2  A: 

For counselor 2, try:

WHERE last_name LIKE 'G%' OR last_name LIKE 'H[a-r]%'

For counselor 3, try:

WHERE last_name LIKE 'H[s-z]%' OR last_name LIKE '[I-O]%'
LittleBobbyTables
It seems so simple when you put it like that ;)Must have more coffee!
Nick DeVore
Now that you mention it, coffee does sound like a good idea. The code should work in your case, but let's pretend for a minute that you had to split up students on the letter O, breaking at Oa-Om and On-Oz. The code above would fail for students like O'Brien, O'Reilly, etc., and you'd probably either want to do a REPLACE to get rid of non-alpha characters, or add them to one of the LIKE statements.
LittleBobbyTables
Good point. One other thing to note is that I put the condition in parenthesis because I do have some other where clauses, and without the parens, it was returning a lot more rows than I wanted.
Nick DeVore

related questions