[Transact-SQL] Filtering and Using Predicates
Online Learning 2020. 12. 9. 16:17WHERE 절에서 지정가능한 Predicate and Operators
Predicates and Operators |
Description |
= < > |
Compares values for equality / non-equality. |
IN |
Determines whether a specified value matches any value in a subquery or a list. |
BETWEEN |
Specifies an inclusive range to test. |
LIKE |
Determines whether a specific character string matches a specified pattern, which can include wildcards. |
AND |
Combines two Boolean expressions and returns TRUE only when both are TRUE. |
OR |
Combines two Boolean expressions and returns TRUE if either is TRUE. |
NOT |
Reverses the result of a search condition. |
UnderScore _ 를 사용해 조건을 만들수도 있다.
A__Z >> A[아무거나][아무거나]Z
DEMOS
SalesLT.Product테이블에서 ProductModelID가 6인 row를 찾아서 Name, Color, Size를 출력한다.
--List information about product model 6
SELECT Name, Color, Size FROM SalesLT.Product WHERE ProductModelID = 6;
LIKE
ProductNumber가 FR로 시작하는 것을 찾는다.
LIKE 'FR%'
--List information about products that have a product number beginning FR
SELECT productnumber,Name, ListPrice FROM SalesLT.Product WHERE ProductNumber LIKE 'FR%';
LIKE
LIKE 'FR-_[0-9][0-9]_-[0-9][0-9]'
FR-[아무거나][0~9까지숫자][0~9까지숫자][아무거나]-[0~9까지숫자][0~9까지숫자] 를 찾는다.
--Filter the previous query to ensure that the product number contains two sets of two didgets
SELECT Name, ListPrice, ProductNumber FROM SalesLT.Product WHERE ProductNumber LIKE 'FR-_[0-9][0-9]_-[0-9][0-9]';
NULL의 취급
SellEndDate가 NULL이 아닌 경우만 출력된다.
--Find products that have no sell end date
SELECT Name, SellEndDate FROM SalesLT.Product WHERE SellEndDate IS NOT NULL;
BETWEEN AND
BETWEEN AND를 사용해서 범위를 지정할 수 있다.
--Find products that have a sell end date in 2006
SELECT Name FROM SalesLT.Product WHERE SellEndDate BETWEEN '2006/1/1' AND '2006/12/31';
IN
IN에는 다양한 값을 한꺼번에 지정가능하다. ProductCategoryID가 5,6,7인 row가 모두 출력된다.
--Find products that have a category ID of 5, 6, or 7.
SELECT ProductCategoryID, Name, ListPrice FROM SalesLT.Product WHERE ProductCategoryID IN (5, 6, 7);
AND
ProductCategoryID가 5,6,7이면서 SellEndDate가 NULL인 것들을 출력한다.
--Find products that have a category ID of 5, 6, or 7 and have a sell end date
SELECT ProductCategoryID, Name, ListPrice, SellEndDate FROM SalesLT.Product WHERE ProductCategoryID IN (5, 6, 7) AND SellEndDate IS NULL;
OR
ProductNumber가 FR로 시작하거나 ProductCategoryID가 5,6,7인 것들이 모두 출력된다.
--Select products that have a category ID of 5, 6, or 7 and a product number that begins FR
SELECT Name, ProductCategoryID, ProductNumber FROM SalesLT.Product WHERE ProductNumber LIKE 'FR%' OR ProductCategoryID IN (5,6,7);
'Online Learning' 카테고리의 다른 글
[영리한 프로그래밍을 위한 알고리즘 강좌] - 순환(Recursion)의 개념과 기본 예제 3 (0) | 2020.12.10 |
---|---|
[영리한 프로그래밍을 위한 알고리즘 강좌] - 순환(Recursion)의 개념과 기본 예제 2 (2) | 2020.12.10 |
[Transact-SQL] Removing Duplicates , Sorting Results, (0) | 2020.12.09 |
[Transact-SQL] Working with NULLs (0) | 2020.12.09 |
[Transact-SQL] Data Types (0) | 2020.12.09 |