Tài liệu Using SQL phần 2 - Pdf 87


Figure 3.6: Products where ProductName is like 'Cha%'
The next SELECT statement uses the LIKE operator to retrieve products where the
ProductName column is like '[ABC]%':
SELECT ProductID, ProductName
FROM Products
WHERE ProductName LIKE '[ABC]%';

Figure 3.7
shows the results of this SELECT statement. LIKE '[ABC]%' matches
products with a name that starts with any of the letters A, B, or C, and ends with any
number of characters.

Figure 3.7: Products where ProductName is like '[ABC]%'
The next SELECT statement uses the LIKE operator to retrieve products where the
ProductName column is like '[^ABC]%':
SELECT ProductID, ProductName
FROM Products
WHERE ProductName LIKE '[^ABC]%';
Figure 3.8
shows the results of this SELECT statement. LIKE '[^ABC]%' matches
products with names that don't start with any of the letters A, B, or C, and end with any
number of characters.

Figure 3.8: Products where ProductName is like '[^ABC]%'
The next SELECT statement uses the LIKE operator to retrieve products where the
ProductName column is like '[A-E]%':
SELECT ProductID, ProductName
FROM Products
WHERE ProductName LIKE '[A-E]%';
Figure 3.9

FROM Products
WHERE ProductID BETWEEN 1 AND 12;
Here's another example that displays the OrderID column for the rows from the Orders
table where the OrderDate is between '1996-07-04' and '1996-07-08':
SELECT OrderID
FROM Orders
WHERE OrderDate BETWEEN '1996-07-04' AND '1996-07-08';

Reversing the Meaning of an Operator
You use the NOT keyword with an operator in a WHERE clause to reverse the meaning
of that operator. For example, the following SELECT statement uses the NOT keyword
to reverse the meaning of the BETWEEN operator:
SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice
FROM Products
WHERE ProductID NOT BETWEEN 1 AND 12; Note You can use the NOT keyword to reverse other operators, for example, NOT LIKE,
NOT IN.
Retrieving Rows with Columns Set to Null
Earlier, I mentioned that columns can contain null values. A null value is different from a
blank string or zero: A null value represents a value that hasn't been set, or is unknown.
You can use the IS NULL operator in a WHERE clause to determine if a column contains
a null value. For example, the following SELECT statement uses the IS NULL operator
to retrieve customers where the Fax column contains a null value:
SELECT CustomerID, CompanyName, Fax
FROM Customers
WHERE Fax IS NULL;
Figure 3.10
shows the results of this SELECT statement.


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status