Professional Documents
Culture Documents
Here is an example:
If you are writing your SELECT statement in a Query Editor or at a command prompt
(PowerShell or else), on the right side of the column name, leave an empty space. Then
type a letter or a word as the alias name of the column. Here are examples:
USE VideoCollection1;
GO
SELECT Title Name, Director Master, Rating Exclusion FROM Videos;
GO
If you want an alias name that uses more than one word and you want the words
separate, you can put them in double-quotes. Here is an example:
USE VideoCollection1;
GO
SELECT Title Name,
Director Master,
Rating Exclusion,
YearReleased "Copyright Year" FROM Videos;
GO
This would produce:
Another solution is to include the alias name between [ and ]. Whether the alias is in one
or many words, you can inlude it in double-quotes or in square brackets [ ].
Instead of using just an empty space between the column name and its alias, you can
type AS. Here are examples:
SELECT FirstName,
LastName,
HomePhone AS PhoneNumber,
ParentsNames AS NamesOfParents
FROM
Registration.Students;
GO
Remember that if the alias is in more than one word and they are separate, you can either
include it in double-quotes or between [ and ]. Here are examples:
SELECT FirstName [First Name],
LastName "Last Name",
HomePhone AS [Phone Number],
ParentsNames AS "Names of Parents"
FROM
Registration.Students;
GO
This would produce:
You can also qualify a column using the name of the table. Here are examples:
SELECT Students.FirstName AS [First Name],
Students.LastName "Last Name",
Students.HomePhone AS [Phone Number],
Students.ParentsNames [Names of Parents]
FROM
Registration.Students;
GO
If so, read the message and click OK. Then click the arrow of the combo box and
select ItemNumber:
15. Press Tab, type Item # andclick somewhere else in the window
separate columns to create a string or a value that is in fact an expression. For example,
you can combine a first name and a last name to produce a full name as an expression.
Another expression can use a date on the table, add a number to it to get a date on
another day. An expression can also be used to perform a calculation on two or more
columns such as employees weekly hours multiplied by their hourly salary to get their
weekly salary.
The most common operator used is the addition. It can be used to combine two or more
strings to get a new one. Here is an example:
SELECT FirstName + N' ' + LastName
FROM
Registration.Students;
GO
This would produce:
The addition can also be used on numeric values. All other arithmetic operators can be
used. For example, you can multiply an employee's weekly time to an hourly salary to get
a weekly salary. The statement of such an expression can be written as follows:
SELECT WeeklyHours * HourlySalary
FROM Payroll
You can also create an alias for an expression to give it the desired name. To do this, on
the right side of the expression, type AS followed by the name. As we learned already, if
the alias is in more than one word, include it in either single quotes or square brackets.
Here is an example:
SELECT FirstName + N' ' + LastName AS N'Full Name',
EmergencyName + N' ' + EmrgPhone AS [Emergency Contact]
FROM
Registration.Students;
GO
This would produce:
In the same way, you can create a longer and more complex expression that contains SQL
keywords, the table's columns, and regular words. Here is an example:
SELECT PropertyType + N' in ' + City + N', ' + State + N', in ' + Condition +
N' condition. Equipped with ' + CAST(Bedrooms AS nvarchar(20)) +
N' bedrooms, ' + CAST(Bathrooms AS nvarchar(20)) +
N' bathrooms. Built in ' + CAST(YearBuilt AS nvarchar(20)) +
N' and selling for ' + CAST(MarketValue AS nvarchar(20))
AS [Property Description]
FROM
Listing.Properties
Here is an example of what this would produce:
Remember that if you are adding strings to each other, you can use the CONCAT()
function.
2. In the Criteria pane, click the first empty box under Column. Type UnitPrice *
DiscountRate / 100 and press Tab
3. In the corresponding Alias box, type Discount Amount
4. Click the next empty box under Column. Type UnitPrice - (UnitPrice *
DiscountRate / 100) and press Tab
5. In the corresponding Alias box, type After Discount
6. To execute, on the main menu, click Query Designer -> Execute SQL
15.
aparts.Deposit,
16.
(aparts.Price + aparts.Deposit) "Due Before Moving",
17.
aparts.Available
18. FROM Presentation.Units aparts;
GO
19. To execute, press F5
To change the name of a column in data selection, on the right side of SELECT, type the
desired name, followed by the assignment operator, followed by the actual name of the
column. If the name you want to use is in one word, simply assign the column name to it.
Here is an example:
SELECT EmergencyName = EmergencyName
FROM
Registration.Students;
GO
If the name you want to use is in more than
one word, you can include it in singlequotes, in double-quotes, or between square
brackets.
6.
7.
8.
9.
Baths = aparts.Bathrooms,
[Monthly Rent] = aparts.Price,
'Primary Deposit' = aparts.Deposit,
(aparts.Price + aparts.Deposit) "Due Before
Moving",
10.
aparts.Available
11. FROM Presentation.Units aparts;
GO
12. Press F5 to execute