When analyzing data, you can ask the database engine to apply one or more filters. To visually create a condition in a Query Designer, in the Criteria pane, click the box at the intersection of the Column's field and the Filter column:
In the SQL, the primary keyword used to create a condition is WHERE. A basic formula to follow is: SELECT WhatField(s) FROM WhatObject WHERE Expression;
We are already familiar with the sections before WHERE. The Expression used in a condition is built using algebraic, logical, and string operators. The Expression is called a criterion. Although a group of expressions, making it plural is called criteria, the word criteria is sometimes used for a singular expression also. The Expression of a WHERE statement is written using the formula: ColumnName Operator Value The ColumnName must be an existing column of a table. It is followed by an appropriate operator as we will see next. Value is the value that will set the condition. To make the statement easier to read, you should include it in parentheses after WHERE.
Depending on how it was created, a field can have actual or null values. In the next sections we will see how to check the value of a field. To check whether a field is holding a null value, use the following formula for the WHERE statement: WHERE ColumnName IS NULL
In this case, only the records that are NULL on the ColumnName will be considered in the result.
Instead of considering the NULL records, the reverse is to get those that are not. To check whether a field doesn't hold a certain value, include the NOT operator in its expression. For example, to check the negativity of an IS NULL expression, use IS NOT NULL to find the records that are not null. In fact example, you can create a list of only records that don't have a null value on a certain column.
As you should know already, the values of a certain column can be null or using a value of type char, varchar, or varchar(max), or their variants (nchar, nvarchar, or nvarchar(max)). The primary Boolean operation you can perform on a field consists of checking its nullity. As mentiond above, this operation can be performed by using IS NULL in its expression. Here is an example: USE master; GO CREATE DATABASE Exercise; GO USE Exercise; GO CREATE TABLE Videos ( [Shelf #] nchar(7) null, Title nvarchar(50) not null, Director nvarchar(50), [Length] int, Rating nchar(10), [Year] int ); GO INSERT INTO Videos VALUES(N'DHE-927', N'Two for the Money', N'D.J. Caruso', 123, N'R', 2008), (N'CGM-683', N'Her Alibi', N'Bruce Beresford', 94, N'PG-13', 1998), (N'FQT-973', N'Memoirs of a Geisha', N'Rob Marshall', 145, N'PG-13', 2006), (N'DBT-395', N'Wall Street', N'Oliver Stone', 126, N'R', 2000); GO INSERT INTO Videos(Title, Director, [Length], Rating) VALUES(N'Stealing Harvard', N'Bruce McCulloch', 85, N'PG-13'); GO INSERT INTO Videos([Shelf #], Title, Director, [Length], [Year]) VALUES(N'TPH-973', N'A Few Good Men', N'Rob Reiner', 138, 1992); GO INSERT INTO Videos(Title, Director, [Year], [Length]) VALUES(N'The Silence of the Lambs', N'Jonathan Demme', 1991, 118); GO INSERT INTO Videos([Shelf #], Title, Director, Rating, [Length]) VALUES(N'DZV-737', N'The Lady Killers', N'Joel Coen & Ethan Coen', N'R', 104); GO INSERT INTO Videos(Title, Director, [Length], Rating, [Year]) VALUES(N'Sneakers', N'Phil Alden Robinson', 126, N'PG-13', 1992), (N'Annie', N'John Huston', 126, N'G', 1982), (N'Dave', N'Ivan Reitman', 110, N'PG-13', 1993); GO INSERT INTO Videos VALUES(N'ADR-737', N'Incredibles (The)', N'Brad Bird', 133, N'PG', 2004); GO SELECT ALL * FROM Videos WHERE [Shelf #] IS NULL; GO This would produce:
As mentioned already, to make the condition easier to read, you should include the expression in parentheses. This would be done as follows: SELECT ALL * FROM Videos WHERE ([Shelf #] IS NULL); GO When the statement executes, the table would display only the records that don't have a value for the state. On the other hand, to get the records that are not null, you would use IS NOT NULL. Here is an example: SELECT ALL * FROM Videos WHERE [Shelf #] IS NOT NULL; GO This would produce:
Another common operation performed on a field consists of finding out whether it holds a specific value. This is done using the equality "=" operator. Therefore, to find out whether a field holds a certain value, compare it with that value. You must include the value in single-quotes. Here is an example: SELECT ALL * FROM Videos
WHERE Rating = N'R';
GO
This would produce:
In a WHERE statement, you can also use the ORDER BY expression to sort a list of records based on a column of your choice. Here is an example: SELECT ALL * FROM Videos WHERE Rating = N'R' ORDER BY Director; GO This would produce:
To check whether a field doesn't hold a certain value, you can use the <> operator. Here is an example: SELECT ALL * FROM Videos
WHERE Rating <> N'R';
GO
Remember (from Lesson 08) that, besides <>, Transact-SQL also supports the != operator used to perform a comparison for inequality. Therefore, the above statement can also be written as: SELECT ALL * FROM Videos
WHERE Rating != N'R';
GO
As an alternative, instead of <> or !=, use the equality operator but precede the expression with NOT. Here is an example: SELECT ALL * FROM Videos
WHERE NOT Rating = N'R';
GO
Notice that the result is the same as if only the comparison for equality was used. Of course, you can precede the <> operation with NOT. Here is an example: SELECT StudentNumber, FirstName, LastName, Gender, ParentsNames FROM Registration.Students WHERE NOT (Gender <> 'Male'); GO In this case, the result would include not the records that are not equal to the value, which would be equivalent to using = and not NOT.
|
|
|||||||||||||||||||||||||||||||||||||||
|
As done for strings, if a field holds both numeric and null values, to find out whether a field holds a null value, apply the IS NULL expression to its condition. Here is an example: SELECT [Shelf #], Title, [Year] FROM Videos WHERE [Year] IS NULL; GO This would produce:
Unlike strings, number-based fields use all Boolean operators supported both by ISO SQL anbd Transact-SQL. They are:
Here is an example: SELECT *
FROM Videos
WHERE [Length] > 125;
GO
This would produce:
There are various ways you can find the negation of a number-based comparison. As seen previously, to negate a comparison, you can precede the expression with the NOT operator. Otherwise, by definition, each Boolean operator has an opposite. They are:
Based on this, to find the negativity of a comparision, you can use the opposite operator.
As you may know already, a Boolean field is one whose type is BIT. A Boolean field can hold only one of two values: 0 or 1 (representing False or True, No or Yes, On or Off). As seen for the other types, a Boolean field can hold a null value if it didn't receive an actual value during data entry. This means that you can compare its value to IS NULL. Here is an example: SELECT StudentNumber, FirstName, LastName, Gender, City, SingleParentHome FROM Registration.Students WHERE SingleParentHome IS NULL; GO This would produce:
In the same way, you can use IS NOT NULL, exactly as done for the other data type, to negate IS NULL. In Microsoft SQL Server, a Boolean or bit value is treated as a small integer. That is, it behaves as if it can use a value from 0 up. In this case, 0 means that the value of the field is false and 1 means the value of the field is true. When filtering Boolean records, you can use the 0 or the 1 value applied to a column with the equality operator. If you use any other number, or although you can also use any integer, you may get get only empty records.
Transact-SQL provides various data types to support dates, times, and combinations of dates and times. Like a string, the value of a date or time is provided or initialized in single-quotes. Unlike a string, the value of a date or time must follow strict rules inside the single-quotes, otherwise the value would be invalid. When performing comparisons on date/time-based fields, you must keep those rules in mind. Because a string-based field can contain anything, its comparison allows only equality or inequality. Unlike strings but like numbers, date and time values support all Boolean operators.
Boolean operators used in date/time fields work as follows:
Here is an example: SELECT StudentNumber, FirstName, LastName, DateOfBirth, Gender, City, SingleParentHome FROM Registration.Students WHERE DateOfBirth >= '1995-01-01'; GO
When it comes to comparisons, date and time values follow the same approach as numbers, but date and time values add some logical rules. To negate a date/time comparison, you can precede it with the NOT operator. If you negate an equality comparison, the expression would mean that the date or time doesn't occur on the indicated value, but it doesn't indicate whether the date/time occurs before or after that date. Therefore, in most cases, to negate a date/time comparison, you should use the opposite of the operator. Because date/time values support all Boolean operators, the opposites of those operators apply in the same logic we reviewed for numeric values:
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|