Google Sheets Query Language: escape apostrophe

2020-03-18 07:37发布

问题:

How can I escape an apostrophe inside a SQL query in Google Sheets?

Here's my function:

=QUERY(QUERY(IMPORTRANGE("XXXXXXX", "XXXXXX!A1:C"),"SELECT * WHERE Col2 = 'Woman's blabla: blablabla'"),"SELECT Col1, Col2")

I've tried adding another apostrophe:

=QUERY(QUERY(IMPORTRANGE("XXXXXXX", "XXXXXX!A1:C"),"SELECT * WHERE Col2 = 'Woman''s blabla: blablabla'"),"SELECT Col1, Col2")

No luck.. and I've tried putting a backslash, no luck as well:

=QUERY(QUERY(IMPORTRANGE("XXXXXXX", "XXXXXX!A1:C"),"SELECT * WHERE Col2 = 'Woman/'s blabla: blablabla'"),"SELECT Col1, Col2")

EDIT: The locale of the document is UK.

回答1:

Try using double quotes around the word with the apostrophe

"SELECT * WHERE Col2 = ""Woman's blabla: blablabla"" "



回答2:

If the string you're trying to match is in a cell, try surrounding the cell name with """" like so:

=QUERY(Foo!A:B,"select A where B = " & """" & A1 & """" & "")



回答3:

Try substituting the double quotes in the source data through the function, you may need to swap out some of the quotes i added - I dont know what the text your searching for actually looks like in your source data:

=QUERY(QUERY(IMPORTRANGE("XXXXXXX", SUBSTITUTE("XXXXXX!A1:C","""","'")),"SELECT * WHERE Col2 = 'Woman's blabla: blablabla'"),"SELECT Col1, Col2")

EDIT: