Search code examples
google-sheetsgoogle-sheets-formula

Dynamic AND statements in google Sheets query


This is in my Google Sheets Cell

=QUERY('BGT-MFA'!A1:AE, "SELECT A,B,C,D,E,F,G,J,K,L,M,N,O,P,Q,R,S,T,U,V WHERE A >= DATE """&TEXT(A2,"yyyy-MM-dd")&""" AND A <= DATE """&TEXT(D2,"yyyy-MM-dd")&""" " & IF(E2 <> "Select Condition", "AND D = '" & E2 & "'", "") &
IF(F2 <> "Select Zone", "AND G = '" & F2 & "'", "") & " ORDER BY A "& G2 &" ", -1)

If I leave the select list in F2 at its default which is "Select Zone" the query runs and I can select any condition by date or return all the data by dates and conditions or all data by selecting the default condition selection. But if I choose a zone even if the zone is in the results it returns no data.


Solution

  • G = '" & F2 & "'"
    

    Within QUERY(), single quotes ' needs to be used only for text match & needs to be disregarded for numerical match as such:

    G = " & F2 & " 
    

    enter image description here