MS SQL Server
  Home arrow MS SQL Server arrow Page 4 - How to Retrieve Data from a Single Table
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 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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 Retrieve Data from a Single Table
By: Bryan Syverson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 26
    2004-06-01

    Table of Contents:
  • How to Retrieve Data from a Single Table
  • SELECT Statement Examples
  • How to Code the SELECT Clause
  • How to Name the Columns in a Result Set
  • How to Code String Expressions
  • How to Code Arithmetic Expressions
  • How to Use Functions
  • How to Eliminate Duplicate Rows
  • How to Return a Subset of Selected Rows
  • How to Code the WHERE Clause
  • How to Use the AND, OR, and NOT Logical Operators
  • How to Use the IN Operator
  • How to Use the BETWEEN Operator
  • 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 Retrieve Data from a Single Table - How to Name the Columns in a Result Set


    (Page 4 of 17 )

    By default, a column in a result set is given the same name as the column in the base table. However, you can specify a different name if you need to. You can also name a column that contains a calculated value. When you do that, the new column name is called a column alias. Figure 4 presents two techniques for creating column aliases.

    The first technique is to code the column specification followed by the AS keyword and the column alias. This is the ANSI-standard coding technique, and it's illustrated by the first example in this figure. Here, a space is added between the two words in the name of the InvoiceNumber column, the InvoiceDate column is changed to just Date, and the InvoiceTotal column is changed to Total. Notice that because a space is included in the name of the first column, it's enclosed in brackets ([]). As you'll learn in chapter 10, any name that doesn't follow SQL Server's rules for naming objects must be enclosed in either brackets or double quotes. Column aliases can also be enclosed in single quotes.

    The second example in this figure illustrates another technique for creating a column alias. Here, the column is assigned to an alias using an equal sign. This technique is available only on SQL Server and is included for compatibility with earlier versions of SQL Server. So although you may see this technique used in older code, I don't recommend it for new statements you write.

    The third example in this figure illustrates what happens when you don't assign an alias to a calculated column. As you can see, no name is assigned to the column, which usually isn't what you want. So you should always be sure to assign a name to any column that's calculated from other columns in the base table.

    Figure 4: Two SELECT statements that name the columns in the result set

    A SELECT statement that uses the AS keyword (the preferred technique):

    SELECT InvoiceNumber AS [Invoice Number], InvoiceDate AS Date,
    InvoiceTotal AS Total
    FROM Invoices

    A SELECT statement that uses the equal operator (an older technique):

    SELECT [Invoice Number] = InvoiceNumber, Date = InvoiceDate,
    Total = InvoiceTotal
    FROM Invoices

    The result set for both SELECT statements:

    bryan

    A SELECT statement that doesn't provide a name for a calculated column:

    SELECT InvoiceNumber, InvoiceDate, InvoiceTotal,
    InvoiceTotal - PaymentTotal - CreditTotal
    FROM Invoices

    bryan

    Description

    • By default, a column in the result set is given the same name as the column in the base table. If that's not what you want, you can specify a column alias or substitute name for the column.
    • One way to name a column is to use the AS phrase as shown in the first example above. Although the AS keyword is optional, I recommend you code it for readability.
    • Another way to name a column is to code the name followed by an equal sign and the column specification as shown in the second example above. This syntax is unique to Transact-SQL.
    • You should always specify an alias for a column that contains a calculated value. If you don't, no name is assigned to it as shown in the third example above.
    • If an alias includes spaces or special characters, you must enclose it in double quotes or brackets ([]). That's true of all names you use in Transact-SQL. SQL Server also lets you enclose column aliases in single quotes for compatibility with earlier releases.

    This is from chapter three of Murach's SQL for SQL Server by Syverson (Murach, ISBN 1-890774-16-2, 2002). Grab a copy at your favorite book store today! Buy this book now.

    More MS SQL Server Articles
    More By Bryan Syverson


     

    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...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway