Literals and Column Names

Written 10/25/2017


Sometimes it can be useful to mix literal values and column names in a single statement. When using SQL Server you are able to rename or create a column within a query, by using an alias. The keyword AS specifies an alias for a column. This can come in handy, when you need to create a column name, which is not part of the table you are working on.

Mixing Literals

Assuming you have the following columns in a datatable named HumanResources.Department from the AdventureWorks database:


If you create the following query:

select GETDATE() as 'Extracted Date',
[AdventureWorks].[HumanResources].[Department].[Name] as 'Department Name',
GroupName NameOfDepartmentGroup
FROM [AdventureWorks].[HumanResources].[Department]

You get the following result:


Notice the names of the columns in the result. Your aliases used in your query, are used for the column names. You can make an alias for any column. So you can control exactly which header you like in the result set.
As you see, the alias keyword is optional. When I provided an alias for the “GroupName” column, I did not provide the AS keyword. You will also notice that the quotes are not required. However, you need to surround the alias name with quotes, if the alias name contains spaces or if it is a reserved word.