Home > other >  SQL how do I add 3 or more conditions into the WHERE clause?
SQL how do I add 3 or more conditions into the WHERE clause?

Time:04-14

I thought by simply adding AND to the WHERE clause, I can infinitely add conditions as long as the selected columns match the WHERE clause. However, the following query:

No matching signature for operator AND for argument types: BOOL, BOOL, STRING. Supported signature: BOOL AND ([BOOL, ...]) at [8:3]

SELECT
  DISTINCT state,
  country,
  city
FROM
  customer_data.customer_address
WHERE
  TRIM(state) = 'OH' 
  AND LENGTH(country) > 2
  AND SUBSTR(city,1,5)

If I were to remove the last line, it shows no error and I can run the query but why would adding another condition return such error?

CodePudding user response:

Yes, you can combine so many logic operations with the WHERE clause but, Your SUBSTR is returning a string so you have to compare it with something, SQL Server in my case tries to act like `Bool so it will show the error message meaning the result is not boolean, but non-boolean:

Msg 4145, Level 15, State 1, Line 13 An expression of non-boolean type specified in a context where a condition is expected, near ')'.

to fix it, you have to compare it with something, let's say the return string is equal to 'abcde'

here is my query using SQL Server:

SELECT
    DISTINCT state,
    country,
    city
FROM
    tbl_Test2
WHERE
    TRIM(state) = 'OH'
    AND  LEN(country) > 2
    AND SUBSTRING(city,1,5) = 'abcde'

My Local Result

  • Related