Where clause with Calculation in T-SQL using VB.NET

The SQL WHERE clause is used to select data conditionally and we can also perform some calulation in this clause.
  • 2387

T-SQL: Transact-SQL (T-SQL) is central to using SQL Server. The Microsoft version of the Structured Query Language is commonly called Transact-SQL, enhances the power of SQL and minimizes the occasions on which users must resort to a programming language to accomplish a desired task.

Where Clause in T-SQL: 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.

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 WHERE Salary - AdvanceSalary > 2500
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> select * from workers WHERE Salary > AdvanceSalary + 4000
3> GO

 ID   Name  BatchNo  JoiningDate   Salary    AdvanceSalary  RemainingSalary
 ---  -------- --------    -------------     --------    --------------    -----------------
 5    Mohan     5         2009-01-10    6000        1000                 5000

(1 rows affected)

Conclusion

I hope this article help you to learn how we can performed calculations with WHERE clause.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.