The idea behind aliasing in SQL statements is to group a set of columns together into a newly named "virtual" column that you refer to in your code. You can also use SQL functions such as SUM, or even concatenate strings, as we will do in this example.

Here is a very simple example of aliasing your SQL statements:
-------------------------------------------------------------------------



Our database (Access2000 in this case), consists of the following fields in a Table named "tblCtcs":
ctcID - autonumber
ctcFname - text
ctcLname - text
ctcBirthday - date/time
-------------------------------------------------------------------------



Note that in our Query below:
1) The database columns "ctcFname" and "ctcLname" are being concatenated and seperated by a space... that's the &' '&
2) " Birthday: " is actually a string included in the statement
3) "AS Name" defines our virtual column that we can refer to simply in our code as... you guessed it, "Name"

<cfquery name="jim" datasource="jimdb">
    SELECT ctcFname &' '& ctcLname &' Birthday: '& ctcBirthday AS Name 
    From tblCtcs
    ORDER BY ctcFname
</cfquery>
-------------------------------------------------------------------------



Based on our SQL query above, we can print out a list of of Contacts, and their Birthdays, by referring to just one variable... the "virtual column" we created, like this:

<ul>
<cfoutput query="jim"><li>#jim.Name#</li></cfoutput>
</ul>

Which would show up in the browser like this:

* Carl Walker Birthday: 5/22/1968
* Diane Summer Birthday: 2/25/1972
* Ehrman Mack Birthday: 10/2/1932
* Jake Summer Birthday: 10/13/1995
* Leena Summer Birthday: 10/6/1998
* Marilyn Lavato Birthday: 4/2/1950
* Norma Mack Birthday: 7/4/1942
* Sonny Summer Birthday: 12/7/1962
-------------------------------------------------------------------------

Thanks a lot and Happy Aliasing!

About This Tutorial
Author: Jim Summer
Skill Level: Intermediate 
 
 
 
Platforms Tested: CF5
Total Views: 34,967
Submission Date: December 10, 2002
Last Update Date: June 05, 2009
All Tutorials By This Autor: 5
Discuss This Tutorial
Advertisement

Sponsored By...
Powered By...