ARTICLE

Use expression in a condition of where clause in T-SQL

Posted by Manish Tewatia Articles | Database & DBA April 06, 2011
Expression is a combination of symbols and operators and WHERE clause is used to select data conditionally, now learn how to use both together.
 
Reader Level:

The SQL WHERE clause is used to select data conditionally, by adding it to already existing SQL SELECT query you can also say it a select statement specifies the search conditions that determine which rows are retrieved. Let see an example in which I performed calculation within where clause.

Expression is a combination of symbols and operators that the SQL Server Database Engine evaluates to obtain a single data value. It is a syntactical element or clause composed of identifiers, operators, and values that can evaluate to obtain a result. Like a sentence consisting of subject, verb, object to convey an action, the expression must be logically complete before it can compute.

Lets look a example, how to use expression in a condition of where clause:

Example:

4>
5>
6> create table workers (
7>     ID                      INTEGER,
8>     Name                 VARCHAR(10),
8>     BatchNo              INTEGER,
9>     JoiningDate         datetime,
10>    Salary                INTEGER,
11>    AdvanceSalary     INTEGER,
12>    RemainingSalary  INTEGER
13>
14> );
15> GO

1>
2> INSERT INTO workers VALUES (1,Rahul, 1, '2010-11-02', 4000, 1500,2500);
3> GO

(1 rows affected)
1> INSERT INTO workers VALUES (2,Ravi, 2, '2009-12-21', 4500, 1500,3000);
2> GO

(1 rows affected)
1> INSERT INTO workers VALUES (3,Shankar, 3, '2011-09-12', 3500, 1000,2500);
2> GO

(1 rows affected)
1> INSERT INTO workers VALUES (2,Ravi, 2, '2009-12-21', 4500, 1500,3000);
2> GO

(1 rows affected)
1> INSERT INTO workers VALUES (5,Mohan, 5, '2009-01-10', 6000,1000,5000);
2> GO

(1 rows affected)


1>
2>
3> select * from workers;
4> GO

 ID   Name   BatchNo   JoiningDate    Salary   AdvanceSalary   RemainingSalary
 ---   ------    ---------   -------------   --------   ------------------ -----------------
 2     Ravi       2            2009-12-21    4500         1500                   3000
 2     Ravi       2            2009-12-21    4500         1500                   3000
 5     Mohan    5            2009-01-10    6000         1000                   5000

(3 rows affected)

1>
2>
3> SELECT Name
4>         FROM workers
5>         WHERE salary * 0.51 > 3000
6> GO

Output:

first_name
----------
 Mohan

(1 rows affected)

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor