# Sort QUERY results to the 4th character of a string

I'm trying to pull a list of people based on their last names. The catch is I only want to QUERY people with last names that fit between an alpha range to the 4th character.

Example

I only want names that fall between Mimz and Sand

I have this formula, but it doesn't go to the 4th character and I don't understand how to update.

=QUERY(data!B1:J30000, "Select B, C, D, E, F, G, H, I, J  where lower(D) matches
'^(m[k-z]|[n-r]|sa).*' order by D")

I believe the adjustment needs to be made here (m[k-z]|[n-r]|sa) but I don't know the syntax.

Any help is greatly appreciated.

Link to copy of report tab

Link to copy of data tab (simplified)

• Please provide some sample data to test your formula and those to be proposed.
– Rubén
Nov 1, 2016 at 23:39
• @Rubén I've added the links to the question. Thanks for looking into this! Nov 2, 2016 at 2:14
• The spreadsheet looks very complex. Please make a simpler demo spreadsheet.
– Rubén
Nov 2, 2016 at 4:46
• @Rubén I've updated link to the "data" sheet and simplified it. I've also removed all other tabs that aren't directly related to this question. Thanks again! Nov 2, 2016 at 15:13

Since this is more a regular expression logic update, I can contribute:

Try:

'^(mimz|mi[n-z]|m[j-z]|[n-r]|sa[a-m]|san[a-d]).*'

Just keep in mind that the range above needs to differ by the first character. If not, then pull the common characters out. For mimz-mitz:

'^mi(mz|[n-s]|t[a-z]).*'

Not saying there isn't an easier way -- this just helps you update this particular way in the future.

• I'm testing this now. Thank you! Is there an easier (more straight forward) way that you recommend? Nov 2, 2016 at 16:04
• First, I don't know how 'dynamic' it needs to be -- this regex is obviously hardcoded. Second, I don't know excel or its myriad of functions. So even if this works (and it should ;) ), someone else might come up with a better answer. That's all I meant. So short answer -- I don't know ;) Nov 2, 2016 at 16:07