IT tutorials
 
Technology
 

SQL Server 2012 : Ordering the Result Set (part 2) - Specifying the ORDER BY Using Column Aliases, Using the Column Ordinal Position , ORDER BY and Collation

10/18/2014 8:42:14 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Specifying the ORDER BY Using Column Aliases

Alternatively, you can use a column alias to specify the columns used in the ORDER BY clause. This is the preferred method for sorting by an expression because it makes the code easier to read. This example sorts in descending order, rather than the default ascending order:

USE AdventureWorks
SELECT LastName + ‘, ‘ + FirstName as FullName
FROM Person.Person
ORDER BY FullName DESC;

Result:

FullName
-------------
Zwilling, Michael
Zwilling, Michael
Zukowski, Jake
Zugelder, Judy
Zubaty, Patricia
Zubaty, Carla
Zimprich, Karin
. . .

An alias is allowed in the ORDER BY clause but not the WHERE clause because the WHERE clause is logically executed prior to processing columns and expressions. The ORDER BY clause follows the assembling of the columns and aliases, so it can use column aliases.

Using the Column Ordinal Position

You can use the ordinal number of the column (column position number) to indicate the ORDER BY columns, but don't do this. If the select columns are changed or their order changes, the sort order also changes.

One case for which it's not necessarily a horrid practice to use the ordinal number to specify the sort is for complex union queries.

The following query demonstrates sorting by ordinal position:

USE AdventureWorks
SELECT LastName + ‘, ‘ + FirstName as FullName
FROM Person.Person
ORDER BY 1;

Result:

FullName
----------------------
Abbas, Syed
Abel, Catherine
Abercrombie, Kim
. . .

ORDER BY and Collation

SQL Server's collation order is vital to sorting data. In addition to determining the alphabet, the collation order also determines whether accents, case, and other alphabet properties are considered in the sort order. For example, if the collation is case-sensitive, then the uppercase letters are sorted before the lowercase letters. The following function reports the installed collation options and the current collation server property:

SELECT * FROM fn_helpcollations();

Result:

name         description
--------------------- -------------------------
Albanian_BIN Albanian, binary sort
Albanian_CI_AI Albanian, case-insensitive,
accent-insensitive,
kanatype-insensitive, width-insensitive
Albanian_CI_AI_WS Albanian, case-insensitive,
accent-insensitive,
kanatype-insensitive, width-sensitive
. . .
SQL_Latin1_General_CP1_CI_AI
Latin1-General,case-insensitive,
accent-insensitive,
kanatype-insensitive, width-insensitive
for Unicode Data, SQL Server Sort Order
54 on Code Page 1252 for non-Unicode
Data
. . .

The following query reports the current server collation:

SELECT SERVERPROPERTY('Collation') AS ServerCollation;

Result:

ServerCollation
------------------------
SQL_Latin1_General_CP1_CI_AS

Although the server collation setting is determined during setup, you can set the collation property for a database or a column using the COLLATE keyword. The following code changes the AdventureWorks database collation so that it becomes case-sensitive:

CREATE DATABASE CollateChange
GO
ALTER DATABASE CollateChange
COLLATE SQL_Latin1_General_CP1_CS_AS;
GO
SELECT DATABASEPROPERTYEX('CollateChange','Collation')
AS DatabaseCollation;

Result:

DatabaseCollation
----------------------------------
SQL_Latin1_General_CP1_CS_AS

Not only can SQL Server set the collation at the server, database, and column levels, collation can even be set at the individual query level. The following query sorts according to the Danish collation, without regard to case or accents:

Use AdventureWorks
SELECT *
FROM Production.Product
ORDER BY Name
COLLATE Danish_Norwegian_CI_AI;

Not all queries need to be sorted, but for those that do, the ORDER BY clause combined with the many possible collations yields tremendous flexibility in sorting the result set.


Terminating the Statement
ANSI SQL uses a semicolon to terminate a statement. Although it's been there as an option for several versions, code with semicolons was unheard of in the SQL Server community until recently. SQL Server 2005 began requiring it for some commands. Therefore, following are the rules about semicolons.
When semicolons are required:
  • At the end of the statement preceding a common table expression (CTE)
  • At the end of a MERGE statement
When not to use a semicolon:
  • Between the END TRY and BEGIN CATCH.
  • Between the IF condition and the BEGIN.
  • Don't mix GO and the semicolon on the same line.
 
Others
 
- SQL Server 2012 : Ordering the Result Set (part 1) - Specifying the ORDER BY Using Column Names, Specifying the ORDER BY Using Expressions
- Microsoft Exchange Server 2013 : Addressing Exchange - The display or Details Templates Editor
- Microsoft Exchange Server 2013 : Addressing Exchange - MailTips and group metrics (part 2) - User experience, Custom MailTips
- Microsoft Exchange Server 2013 : Addressing Exchange - MailTips and group metrics (part 1) - Client interaction, Configuring MailTips
- Sharepoint 2013 : Wikis
- Sharepoint 2013 : Blogs
- Sharepoint 2013 : Ratings
- Sharepoint 2013 : My Sites (part 5) - My Content
- Sharepoint 2013 : My Sites (part 4) - My Newsfeed
- Sharepoint 2013 : My Sites (part 3) - My User Profile Page
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us