Tuesday, January 27, 2015

SELECT dữ liệu với các toán tử .

USE AdventureWorks2008;

-- = (Equal to)
SELECT 'Equal to' WHERE 5 = 3+2

-- != (Not equal to)
SELECT 'Not equal to' WHERE 5 != 3+1
SELECT 'Not equal to' WHERE 5 <> (select count(*)
       from HumanResources.Department)

-- > (Greater than)
SELECT 'Greater than' WHERE (select COUNT(*) from HumanResources.Employee)
     > (select count(*) from HumanResources.Department)
    
-- >= (Greater than or equal to)
SELECT 'Greater than or equal to' WHERE (select COUNT(*)
        from HumanResources.Employee)  >= (select count(*) from
        HumanResources.Department)

-- SQL less than
SELECT 'Less than' WHERE (select COUNT(*) from HumanResources.Employee)
     < 400 -- < (Less than)
    
-- SQL less than or equal to
SELECT 'Less than or equal to' WHERE 10  <= (select count(*) from
        HumanResources.Department)

-- in (Equal to any list member)
SELECT 'In operator' WHERE (select count(*) from
        HumanResources.Department) in (15, 16, 17)

-- not in (Not equal to any list member)
SELECT 'Not in operator' WHERE (select count(*) from HumanResources.Department)
        not in (15, 17)

-- between  (Inclusive between two values)
SELECT 'Between' WHERE YEAR(getdate()) between 2009 and 2025

-- not between  (Inclusive not between two values)
SELECT 'Not between' WHERE 11 not between 1 and 10

-- OR operator - logical & bitwise OR
-- Bitwise OR
SELECT 2 | 16  -- 18
-- Logical OR
SELECT 'OR operator' WHERE 5=6 OR 10=2*5

-- AND operator - logical & bitwise AND
-- Bitwise AND
SELECT 8 & 31  -- 8
-- Logical AND
SELECT 'AND operator' WHERE 2*2=4 AND (YEAR(GETDATE()) BETWEEN 2009 AND 2025)

Source : sqlusa

No comments:

Post a Comment