Logo 
Search:

SQL Server Articles

Submit Article
Home » Articles » SQL Server » MiscellaneousRSS Feeds

Creating Column Aliases with AS clause

Posted By: Sarita Patel     Category: SQL Server     Views: 4067

This article explains about creating column aliases with AS clause in sql server.

AS clause is used to create a column alias to display in result. Column alias is an alternative name that you specify to control how column headings are displayed in a result. Generally column aliases are used if column names are too long, too short, hard to type or cryptic.


To create column aliases :

Syntax of creating column aliases in select query :

 SELECT column1  [AS] alias1,

                column2  [AS] alias2,

                …..

                columnN  [AS] aliasN

FROM     table


Column alias immediately follows a column name in the SELECT clause of a SELECT statement.

For example :

SELECT contactname AS CustomerName

FROM Customers

 

Enclose the alias in single or double quotes if it’s a reserved keyword or if it contains a spaces, puntualtion or special characters.

For example : 

SELECT contactname AS ‘User Name’ , customerid AS “User ID”, city AS City

FROM     Customers

 

Note that Column aliase doesn’t change the name of a column in a table. As is also used to name derived columns i.e. whose values are determined  by expressions

For example : 

SELECT ProductName, UnitsInStock * UnitPrice AS TotalAmount

FROM products   

  


Example of creating column aliases in select query :

SELECT ContactName AS 'Customer Name',

                PostalCode AS "Post Code",

                country AS Country

FROM     Customers

 

Output

Customer Name          Post Code     Country

Maria Anders               12209           Germany

Ana Trujillo                 05021           Mexico

Antonio Moreno          05023           Mexico

Thomas Hardy              WA1 1DP    UK

Christina Berglund      S-958 22       Sweden


As in above example you can note that all column names are changed by giving alias using AS clause. 

  
Share: 

 
 

Didn't find what you were looking for? Find more on Creating Column Aliases with AS clause Or get search suggestion and latest updates.

Sarita Patel
Sarita Patel author of Creating Column Aliases with AS clause is from United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!