BETWEEN ... AND
The BETWEEN ... AND operator selects a range of data between two values. These values can be numbers, text, or dates.
SELECT column_name FROM table_name |
Original Table (used in the examples)
LastName | FirstName | Address | City |
---|---|---|---|
Hansen | Ola | Timoteivn 10 | Sandnes |
Nordmann | Anna | Neset 18 | Sandnes |
Pettersen | Kari | Storgt 20 | Stavanger |
Svendson | Tove | Borgvn 23 | Sandnes |
Example 1
To display the persons alphabetically between (and including) "Hansen" and exclusive "Pettersen", use the following SQL:
SELECT * FROM Persons WHERE LastName |
Result:
LastName | FirstName | Address | City |
---|---|---|---|
Hansen | Ola | Timoteivn 10 | Sandnes |
Nordmann | Anna | Neset 18 | Sandnes |
IMPORTANT! The BETWEEN...AND operator is treated differently in different databases. With some databases a person with the LastName of "Hansen" or "Pettersen" will not be listed (BETWEEN..AND only selects fields that are between and excluding the test values). With some databases a person with the last name of "Hansen" or "Pettersen" will be listed (BETWEEN..AND selects fields that are between and including the test values). With other databases a person with the last name of "Hansen" will be listed, but "Pettersen" will not be listed (BETWEEN..AND selects fields between the test values, including the first test value and excluding the last test value). Therefore: Check how your database treats the BETWEEN....AND operator!
Example 2
To display the persons outside the range used in the previous example, use the NOT operator:
SELECT * FROM Persons WHERE LastName |
Result:
LastName | FirstName | Address | City |
---|---|---|---|
Pettersen | Kari | Storgt 20 | Stavanger |
Svendson | Tove | Borgvn 23 | Sandnes |
No comments:
Post a Comment