Tuesday, May 13, 2014

TSQL (Transact) Tutorial: IN Operator

In TSQL IN operator behaves like OR condition by reducing multiple steps for OR in one statement.

expression IN (value1, value2, .... value_n);

In IN operator you need to provide at least one value to make it work. For string value the values need to be provided between single quotes while for numeric values single quotes are not required.

Using - with string Values

Let say we want to fetch data from Employee table where Employee belongs to mumbai, Delhi and Gurgaon. If we want to perform this with or operator we need to write three statement with condition while by using in its possible in one condition only.

FROM employees
WHERE City IN ('Mumbai', 'Delhi', 'gurgaon');

Using - With Numeric

Using above mentioned scenario now we are filtering on basis of city id

FROM employees
WHERE City_id IN (1, 2, 3);

However it’s not possible that all scenario which we can cover with OR is possible in IN.

No comments:

Post a Comment