Search code examples
google-sheetswildcardapostrophe

Using wildcard characters and escaping apostophes in Google Sheets Query


I am trying to do a query which pulls a date based on a name from a cell that contains many names separated by commas that was created by a Google form.

This using the wildcards will pull all the dates from any name that is in B except give an error when the name contains an apostrophe:

=QUERY("SOURCE", "select max(D) where (B like '%"&$A$7&"%') Label max(D) ''", 0)

Alternatively, often times B only has a single name so I can run the following query and pull the date I need if B only has a single name even with an apostrophe.

=QUERY("SOURCE", "select max(D) where (B like """&$A$7&""") Label max(D) ''", 0)

What I can't figure out is how to use the wildcard characters and escape the apostrophe at the same time.


Solution

  • Maybe filter() is easier to use in this situation? Assuming names are separated with a comma (and a space), try

    =if(len(G4)*len(F6), max(filter(B3:B, regexmatch(A3:A, G6), regexmatch(C3:C, F6))),)
    

    Change ranges to suit. If that doesn't help, please share an example spreadsheet.