MS SQL Server
  Home arrow MS SQL Server arrow Page 10 - 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 
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 Retrieve Data from a Single Table
By: Bryan Syverson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 27
    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 Code the WHERE Clause


    (Page 10 of 17 )

    Earlier in this chapter, I mentioned that to improve performance, you should code your SELECT statements so that they retrieve only the columns you need. That goes for retrieving rows too: The fewer rows you retrieve, the more efficient the statement will be. Because of that, you'll almost always include a WHERE clause on your SELECT statements with a search condition that filters the rows in the base table so that only the rows you need are retrieved. In the topics that follow, you'll learn a variety of ways to code this clause.

    How to Use Comparison Operators

    Figure 10 shows you how to use the comparison operators in the search condition of a WHERE clause. As you can see in the syntax summary at the top of this figure, you use a comparison operator to compare two expressions. If the result of the comparison is True, the row being tested is included in the query results.

    The examples in this figure show how to use some of the comparison operators. The first WHERE clause, for example, uses the equal operator (=) to retrieve only those rows whose VendorState column have a value of IA. Notice that because the state code is a string literal, it must be included in single quotes. In contrast, the numeric literal used in the second WHERE clause is not enclosed in quotes. This clause uses the greater than (>) operator to retrieve only those rows that have a balance due greater than zero.

    The third WHERE clause illustrates another way to retrieve all the invoices with a balance due. Like the second clause, it uses the greater than operator. Instead of comparing the balance due to a value of zero, however, it compares the invoice total to the total of the payments and credits that have been applied to the invoice.

    The fourth WHERE clause illustrates how you can use comparison operators other than equal with string data. In this example, the less than operator (<) is used to compare the value of the VendorName column to a literal string that contains the letter M. That will cause the query to return all vendors with names that begin with the letters A through L.

    You can also use the comparison operators with date literals, as illustrated by the fifth and sixth WHERE clauses. The fifth clause will retrieve rows with invoice dates on or before May 31, 2002, and the sixth clause will retrieve rows with invoice dates on or after May 1, 2002. Notice that, like string literals, date literals must be enclosed in single quotes. Also notice that the two literals specify the dates using different formats. You'll learn more about the acceptable date formats in chapter 8.

    The last WHERE clause shows how you can test for a not equal condition. To do that, you code a less than sign followed by a greater than sign. In this case, only rows with a credit total that's not equal to zero will be retrieved.

    Figure 10: The syntax of the WHERE clause with comparison operators

    WHERE expression_1 operator expression_2

    The comparison operators:

    = Equal
    > Greater than
    < Less than
    <= Less than or equal to
    >= Greater than or equal to
    <> Not equal

    Examples of WHERE clauses that retrieve...

    Vendors located in Iowa
    WHERE VendorState = 'IA'

    Invoices with a balance due (two variations)
    WHERE InvoiceTotal - PaymentTotal - CreditTotal > 0
    WHERE InvoiceTotal > PaymentTotal + CreditTotal

    Vendors with names from A to L
    WHERE VendorName < 'M'

    Invoices on or before a specified date
    WHERE InvoiceDate <= '2002-05-31'

    Invoices on or after a specified date
    WHERE InvoiceDate >= '5/1/02'

    Invoices with credits that don't equal zero
    WHERE CreditTotal <> 0

    Description

    • You can use a comparison operator to compare any two expressions that result in like data types. Although unlike data types may be converted to data types that can be compared, the comparison may produce unexpected results.
    • If the result of a comparison results in a True value, the row being tested is included in the result set. If it's False or Unknown, the row isn't included.
    • To use a string literal or a date literal in a comparison, enclose it in quotes. To use a numeric literal, enter the number without quotes.
    • Character comparisons performed on SQL Server databases are not case-sensitive. So, for example, 'CA' and 'Ca' are considered equivalent.

    Whenever possible, you should compare expressions that have similar data types. If you attempt to compare expressions that have different data types, SQL Server may implicitly convert the data type for you. Often, this implicit conversion is acceptable. However, implicit conversions will occasionally yield unexpected results. In that case, you can use the CONVERT function you saw earlier in this chapter or the CAST function you'll learn about in chapter 8 to explicitly convert data types so the comparison yields the results you want.

    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 6 hosted by Hostway
    Stay green...Green IT