SQL Server Home @ it-notebook.org

How to...retrieve data based on several conditions

(Kristofer Gafvert, October 13, 2009)

Usually when you want to see data from a table, you do not want all data, but instead data that satisfy a criteria. The following example retrieves addresses from the Person.Address table in the AdventureWorks sample database, where the city is "Bothell".

SELECT *
FROM Person.Address
WHERE (City = 'Bothell' OR City = 'Phoenix')
      AND ModifiedDate > '2001-01-01'

Remember that text must be enclosed with apostrophes (' character).

In the above example, the WHERE clause is used to restrict rows returned by the query. In this case, only addresses in either Bothell or Phoenix are returned, but only if it has been modified after 2001-01-01.

The WHERE clause consists of expressions that evaluate to TRUE, FALSE or UNKNOWN. An expression evaluates to UNKNOWN when two NULL values are compared. That is because two NULL-values are not equal, it just tells us that the value is not known.

Applies to [?]

SQL Server 2008

See also

How to... search for NULL values