Aliasing Field Names with AS

Aliasing Field Names with AS

SQL gives you the ability to alias, or rename, a field or expression in a query. (We used this capability in both examples in the preceding section.) You typically need to alias a field for two reasons:

  1. The underlying table has field names that are unwieldy, and you want to make the field names in the result set easier to deal with

  2. The query that you're creating produces some sort of calculated or aggregated column that requires a name

Whatever your reason for wanting to alias a field name, it's easy to do with the AS clause in SQL. For example, say that you're doing a complex series of calculations to determine the extended price on invoices (the extended price is the item price multiplied by the quantity shipped). You also want to refer to the calculated column as ExtendedPrice. You can do so by writing the SQL code

 SELECT TOP 5 ItemID, Quantity, Price,    tblInventory Retail Price * tblOrderItem.Quantity AS ExtendedPrice FROM tblOrderItem INNER JOIN   tblInventory ON tblOrderItem.ItemID = tblItem.ID 

This query produces the following result set.

ItemID

Quantity

Retail Price

ExtendedPrice

1

1

5.99

5.99

2

2

1.39

2.78

5

3

2.29

6.87

4

2

3.99

7.98

7

1

5.99

5.99

The entries in the ExtendedPrice field aren't stored in the database; they're calculated on the fly.



Database Access with Visual Basic. NET
Database Access with Visual Basic .NET (3rd Edition)
ISBN: 0672323435
EAN: 2147483647
Year: 2003
Pages: 97

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net