To visually create a BETWEEN expression, in the Criteria pane, click the box of the column on which the operation will be performed and type the expression. The statement is in the form: BETWEEN Start AND End In the SQL, the BETWEEN operator is used in a WHERE condition and combines with an AND conjunction. The basic formula to use a BETWEEN operator in SQL is: WHERE Expression BETWEEN Start AND End The Expression placeholder is usually the name of the column whose values you want to examine. Start is the starting value of the range to consider. End is the highest value to consider in the range. After this condition is executed, it produces the list of values between Start and End. The values used in a BETWEEN operation must be of the same type and the SQL interpreter must be able to identify the range of values:
Of course, when creating a BETWEEN operation, you should be able to perform the operation on only similar values.
To negate a BETWEEN operation, you can precede it with NOT. Here is an example: SELECT * FROM Students WHERE not Grade BETWEEN N'a' AND N'c'; This would produce:
To make the statement easier to read, you should include in parentheses the section after NOT. Here is an example: SELECT * FROM Students WHERE NOT (Grade BETWEEN N'a' AND N'c'); An alternative is to use the ranges outside the desired one, such as BETWEEN N'd' AND N'z'.
You can combine two or more BETWEEN operation to consider more than one range of values. If you want to combine two BETWEEN operation, you can use the following formula: BETWEEN Start1 AND End1 OR Start2 AND End2 To make the statement easier to read, you should include each operation in parentheses: BETWEEN (Start1 AND End1) OR (Start2 AND End2) Each operation is evaluated individually. The first range is considered, followed by the second. Then the records resulting from each operation is combined to the other. Here is an example: SELECT * FROM Students WHERE (StudentNumber BETWEEN 1002 AND 1004) OR (StudentNumber BETWEEN 1008 AND 1010); This would produce:
In the above statement, examine the ranges from a single column. You can create one BETWEEN operation from one column and another operation from another column, then combine them. Here is an example: SELECT * FROM Students WHERE (StudentNumber BETWEEN 1002 AND 1004) OR (Grade BETWEEN 'A' AND N'B'); This would produce:
|
|
|||||||||||||||||||||||
|
Imagine you have a field that holds certain repeating values. Here is an example we used: USE Exercise; GO CREATE TABLE Employees([Empl #] nchar(10), [First Name] nvarchar(20), [Last Name] nvarchar(20), Salary money, [Full Time?] bit); GO INSERT INTO Employees VALUES(N'29730', N'Philippe', N'Addy', 20.05, 1); GO INSERT INTO Employees([Empl #], [First Name], [Last Name], Salary) VALUES(N'28084', N'Joan', N'Shepherd', 12.72); GO INSERT INTO Employees([Empl #], [First Name], [Last Name], Salary) VALUES(N'79272', N'Joshua', N'Anderson', 18.26); GO INSERT INTO Employees VALUES(N'22803', N'Gregory', N'Swanson', 15.95, 0); GO INSERT INTO Employees([Empl #], [Last Name], Salary, [Full Time?]) VALUES(N'28084', N'Shepherd', 12.72, 1), (N'39742', N'Anders', 8.88, 0); GO INSERT INTO Employees VALUES(N'83084', N'Josephine', N'Anderson', 20.02, 1); GO INSERT INTO Employees([Empl #], [First Name], [Last Name], Salary) VALUES(N'79272', N'James', N'Anders', 18.26), (N'27924', N'Gregory', N'Hope', 12.85), (N'39742', N'John', N'Anderson', 8.88); GO SELECT * FROM Employees; GO This would produce:
Notice that some names (such as Sheperd, Anders, and Anderson). We already know how to fnd records that share a certain value. For example, the following code gives us the list of employees whose last name is Anderson: SELECT * FROM Employees WHERE [Last Name] = N'Anderson'; This would produce:
Imagine you want to add another value to your seach. We already know how to do that with an OR operator. Here is an example: SELECT * FROM Employees WHERE ([Last Name] = N'Anderson') OR ([Last Name] = N'Shepherd'); This would produce:
In the same way, you can add as many OR expressions as you see fit. As seen previously, you can create a logical disjunction that deals with either only one or more than one column. Here is an example that acts on two columns: SELECT * FROM Employees WHERE ([Last Name] = N'Anderson') OR ([Full Time?] IS NOT NULL); This would produce:
If your logical disjunction involves only one column, the SQL provides an alternative operator named IN.
The IN operation behaves like an OR applied to onw column but there are different ways you created each. You can create the expression visually or with code. First, the formula of an IN operation is: IN(Expression1, Expression2, Expression_n) You start with the IN keyword and add some parentheses to it. In the parentheses, put the list of values separated by commas. To visually create an IN operation, open the table in the Query Designer. In the Filter box that corresponds to the column, type the expression. In the SQL, the IN operator is associated with a WHERE statement. The formula to follow is: WHERE ColumnName IN(Expression1, Expression2, Expression_n) Start with WHERE followed by the name of the column that holds the values to look for. In the parentheses of IN, add the desired values separated by commas. Each Expression can be one of the values of the ColumnName. This is equivalent to Expression1 OR Expression2 OR Expression_n, etc.
As seen for the other operators, there are various ways you can negate an IN operator. Consider the following example: SELECT * FROM Employees WHERE [Last Name] IN (N'Anderson', N'Shepherd');
The easiest way is to precede the operation with NOT. One solution is to put NOT before the name of the column. Here is an example: SELECT * FROM Employees WHERE NOT [Last Name] IN (N'Anderson', N'Shepherd');
Another solution is to precede the IN keyword with NOT. This can be done as follows: SELECT * FROM Employees WHERE [Last Name] NOT IN (N'Anderson', N'Shepherd'); If the number of values in the column is short, to negate an IN operation, instead of the desired values, you can give the other values.
To refine your data analysis, you can use functions, whether functions you create yourself or Transact-SQL built-in functions. As always mentioned, the first candidates of functions you should use are the built-in functions. To use a built-in function, in the placeholder of the column, type the name of the function, followed by its parentheses. If the function takes some parameters, remember to follow the rules to call a parameterized function. Here is an example that uses some date-based built-in functions to display the ages of the students: SELECT FirstName, LastName, Gender, DATEDIFF(year, DateOfBirth, GETDATE()) AS Age FROM Registration.Students This would produce:
You can also include a function in any of the operators we have reviewed so far. Here is an example: SELECT FirstName, LastName, Gender, DateOfBirth, SingleParentHome FROM Registration.Students WHERE (DateOfBirth BETWEEN CONVERT(datetime2, N'1995-01-01', 102) AND CONVERT(datetime2, N'1999-12-31', 102)) You can also involve a built-in function in an expression.
If none of the built-in functions satisfies your needs, you can create your own and use it during data analysis. Obviously, you should first create the function. Here is an example of two functions created in the ROSH database: USE ROSH; GO /* ============================================= Author: FunctionX Create date: Friday 6 April, 2007 Description: This function is used to get the full name of a student ============================================= */ CREATE FUNCTION Registration.GetFullName ( @FName nvarchar(20), @LName nvarchar(20) ) RETURNS varchar(41) AS BEGIN RETURN @LName + ', N' + @FName; END; GO /* ============================================= Author: FunctionX Create date: Saturday 7 April, 2007 Description: This function is used to display Yes or No ============================================= */ CREATE FUNCTION Registration.ShowYesOrNo ( @SingleParentHomeStatus bit ) RETURNS nvarchar(3) AS BEGIN DECLARE @Result nvarchar(3); IF @SingleParentHomeStatus = 0 SET @Result = N'No'; ELSE SET @Result = N'Yes'; RETURN @Result; END; GO Once a function is ready, in the placeholder of your SQL statement, type the name of the schema, followed by a period, followed by the name of the function, its parentheses, and its paremeter(s), if any, inside of the parentheses. Here is an example: SELECT StudentID, Registration.GetFullName(FirstName, LastName) AS [Student's Name], Gender, Registration.ShowYesOrNo(SingleParentHome) AS [Live's in a Single Parent Home?], ParentsNames AS [Parents' Names] FROM Registration.Students; GO This would produce:
|
|
|||||||||||||||||||||||||
|