Use expression in a condition of where clause in T-SQL with VB.NET

Expression is a combination of symbols and operators and WHERE clause is used to select data conditionally, now learn how to use both together.
  • 2478

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)

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.