Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with AND and OR clauses

Status
Not open for further replies.

Geee

Programmer
Apr 23, 2001
253
GB
I have the following statement that apparently is incorrect...

SELECT * FROM dbo.products WHERE (CategoryID LIKE '%') AND (ProductName LIKE '%%') AND ((CategoryID <> '') OR (CategoryID = '1 ')) ORDER BY ProductName

Basically I want to select all from the products table, where CategoryID contains % and ProdunctName contains %. IN addition to this the CategoryID should be not equal to nothing, or should be 1. I thought the brackets in the above statement would convey this. ANyone actually understand what I'm on about???

G -GTM Solutions, Home of USITE-
-=
 

% is a wildcard character in SQL. If you want to find a literal % in a column enclose the % in brackets.

Example: find a % anywhere in the column.
Like '%[%]%'

Note: If CategoryID contains % it cannot = '' OR 1. If CategoryID is a number, it doesn't make sense to search for % in the column.

I'm not sure where to go from here because I'm unclear about your criteria. Perhaps you can clarify. It would be helpful to see examples of the data. Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
A couple of observations:

If you are using Oracle, '%' is a wildcard; consequently, saying
WHERE categoryID LIKE '%' is the same as saying
' any categoryID'.

Secondly, since (categoryID = 1) is a subset of (categoryID <>''), you don't need this second condition.

In essence, if you are using Oracle, your query can be replaced by
SELECT * FROM dbo.products
ORDER BY ProductName;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top