DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to apply SQL logical operators?

Execute the following scripts in SSMS Query Editor to demonstrate the application of Microsoft SQL Server logical operators.

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)

 

Related articles:

 

& (Bitwise AND) (Transact-SQL)

 

Operators 

 

SQL Server Logical Bitwise Operators

 

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE