Aliasing columns in a SELECT statement example


By:
Overview

The purpose for aliasing a column is to have a friendly name for the column.  This name might differ from the physical design to business terms or remove issues such as spacing in the column name.  Let's take a look at an example. 

Explanation
Let's use the same query from an earlier tutorial with one minor modification for the column alias.  In the example below, we are selecting the LoginID column from the HumanResources.Employee table where the VacationHours column equals 8 and we are ordering the data by the HireDate in ascending order which is implied.  What is different is that we are aliasing the LoginID column as 'Domain\LoginName' to be a little bit more specific.

USE AdventureWorks;
GO
SELECT LoginID AS 'Domain\LoginName'
FROM HumanResources.Employee
WHERE VacationHours = 8
ORDER BY HireDate;
GO

Below is the sample result set, which shows the column name returned as 'Domain\LoginName' (alias) rather than LoginID (physical design):

lumnAlias


Last Update: 3/9/2009




Comments For This Article

















get free sql tips
agree to terms