Field calculations

SQL  allows you to calculate the fields of a table. The basic arithmetic operations to calculate the fields are addition(+), subtraction(-), multiplication(*), and division(/).
Example: Calculate the sub totals of each product in the TblProduct below:


TblProduct table:

ProID ProName UnitPrice Qty CatID
P001Angkor 20$ 10 Cat001
P002Anchor 19$ 15 Cat002
P003ABC 50$ 8 Cat003

SELECT ProID,UnitPrice*Qty AS Subtotal

FROM TblProduct;

Result:

ProID Subtotal
P001200
P002285
P003400
 

Note: We use the AS keyword to assign the new name(Subtotal) to the new field that derive from the calculation of other fields. This is called aliasing the field name.


HTML Comment Box is loading comments...



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.