MS SQL Server
  Home arrow MS SQL Server arrow Page 5 - How To Receive Data from a Single Table, c...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

How To Receive Data from a Single Table, concluded
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2006-03-09

    Table of Contents:
  • How To Receive Data from a Single Table, concluded
  • How to use the LIKE operator
  • How to use the IS NULL clause
  • How to code the ORDER BY clause
  • How to sort a result set by an alias, an expression, or a column number

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    How To Receive Data from a Single Table, concluded - How to sort a result set by an alias, an expression, or a column number


    (Page 5 of 5 )


     
    Figure 3-17 presents three more techniques you can use to specify sort columns. First, you can use a column alias that’s defined in the SELECT clause. The first SELECT statement in this figure, for example, sorts by a column named Address, which is an alias for the concatenation of the VendorCity, VendorState, and VendorZipCode columns. Notice that within the Address column, the result set is also sorted by the VendorName column.

    You can also use an arithmetic or string expression in the ORDER BY clause, as illustrated by the second example in this figure. Here, the expression consists of the VendorContactLName column concatenated with the VendorContactFName column. Notice that neither of these columns is included in the SELECT clause. Although SQL Server allows this seldom-used coding technique, many other systems do not.

    The last example in this figure shows how you can use column numbers to specify a sort order. To use this technique, you code the number that corresponds to the column of the result set, where 1 is the first column, 2 is the second column, and so on. In this example, the ORDER BY clause sorts the result set by the second column, which contains the concatenated address, then by the first column, which contains the vendor name. The result set returned by this statement is the same as the result set returned by the first statement. Notice, however, that the statement that uses column numbers is more difficult to read because you have to look at the SELECT clause to see what columns the numbers refer to. In addition, if you add or remove columns from the SELECT clause, you may also have to change the ORDER BY clause to reflect the new column positions. As a result, you should avoid using this technique.

    Figure 3-17.  How to sort a result set by an alias, an expression, or a column number

    An ORDER BY clause that uses an alias

      SELECT VendorName, 
       
      VendorCity + ', ' + VendorState + ' ' + VendorZipCode AS Address
      FROM Vendors
      ORDER BY Address, VendorName

    An ORDER BY clause that uses an expression

      SELECT VendorName,
         
    VendorCity + ', ' + VendorState + ' ' + VendorZipCode AS Address
      FROM Vendors
      ORDER BY VendorContactLName + VendorContactFName

    An ORDER BY clause that uses column positions

      SELECT VendorName,
         
    VendorCity + ', ' + VendorState + ' ' + VendorZipCode AS Address
      FROM Vendors
      ORDER BY 2, 1

    Description

    • The ORDER BY clause can include a column alias that’s specified in the SELECT clause.
    • The ORDER BY clause can include any valid expression. The expression can refer to any column in the base table, even if it isn’t included in the result set.
    • The ORDER BY clause can use numbers to specify the columns to use for sorting. In that case, 1 represents the first column in the result set, 2 represents the second column, and so on.

       

    Perspective
     
    The goal of this chapter has been to teach you the basic skills for coding SELECT statements. You’ll use these skills in almost every SELECT statement you code. As you’ll see in the chapters that follow, however, there’s a lot more to coding SELECT statements than what’s presented here. In the next three chapters, then, you’ll learn additional skills for coding SELECT statements. When you complete those chapters, you’ll know everything you need to know about retrieving data from a SQL Server database.

    Terms

    keyword

    literal value

    comparison operator

    filter

    string literal

    logical operator

    Boolean expression

    string constant

    compound condition

    predicate

    arithmetic expression

    subquery

    expression

    arithmetic operator

    string pattern

    column alias

    order of precedence

    mask

    substitute name

    function

    wildcard

    string expression

    parameter

    null value

    concatenate

    argument

    nested sort

    concatenation operator

    date literal

     


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Murach's SQL for SQL Server," published by...
     

    Buy this book now. This article is excerpted from chapter three of the book Murach's SQL for SQL Server, written by Bryan Sylverson (Murach; ISBN: 1890774162). Check it out today at your favorite bookstore. Buy this book now.

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT