MS SQL Server
  Home arrow MS SQL Server arrow How To Receive Data from a Single Table, c...
Moblin
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 
Actuate Whitepapers 
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 Receive Data from a Single Table, continued
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-03-02

    Table of Contents:
  • How To Receive Data from a Single Table, continued
  • 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

  • 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

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    How To Receive Data from a Single Table, continued


    (Page 1 of 6 )

    If you have ever wondered how to code SELECT statements, this article is for you. It is the second of three parts, excerpted from chapter three of the book Murach's SQL for SQL Server, written by Bryan Sylverson (Murach; ISBN: 1890774162).

    How to use functions

    Figure 3-7 introduces you to functions and illustrates how you use them in column specifications. A function performs an operation and returns a value. For now, don’t worry about the details of how the functions shown here work. You’ll learn more about all of these functions in chapter 8. Instead, just focus on how they’re used in column specifications.

    To code a function, you begin by entering its name followed by a set of parentheses. If the function requires one or more parameters, you enter them within the parentheses and separate them with commas. When you enter a parameter, you need to be sure it has the correct data type. You’ll learn more about that in chapter 8.

    The first example in this figure shows how to use the LEFT function to extract the first character of the VendorContactFName and VendorContactLName columns. The first parameter of this function specifies the string value, and the second parameter specifies the number of characters to return. The results of the two functions are then concatenated to form initials as shown in the result set for this statement.

    The second example shows how to use the CONVERT function to change the data type of a value. This function requires two parameters. The first parameter specifies the new data type, and the second parameter specifies the value to convert. In addition, this function accepts an optional third parameter that specifies the format of the returned value. The first CONVERT function shown here, for example, converts the PaymentDate column to a character value with the format mm/dd/yy. And the second CONVERT function converts the PaymentTotal column to a variable-length character value that’s formatted with commas. These functions are included in a string expression that concatenates their return values with the InvoiceNumber column and three literal values.

    The third example uses two functions that work with dates. The first one, GETDATE, returns the current date. Notice that although this function doesn’t accept any parameters, the parentheses are still included. The second function, DATEDIFF, gets the difference between two date values. This function requires three parameters. The first one specifies the units in which the result will be expressed. In this example, the function will return the number of days between the two dates. The second and third parameters specify the start date and the end date. Here, the second parameter is the invoice date and the third parameter is the current date, which is obtained using the GETDATE function.

    Figure 3-7.   How to use functions

    A SELECT statement that uses the LEFT function

      SELECT VendorContactFName, VendorContactLName,
         LEFT(VendorContactFName, 1) +
         LEFT(VendorContactLName, 1) AS Initials
    FROM Vendors

    A SELECT statement that uses the CONVERT function

      SELECT 'Invoice: #' + InvoiceNumber
         + ', dated ' + CONVERT(char(8), PaymentDate, 1)
        
    + ' for $' + CONVERT(varchar(9), PaymentTotal, 1)
      FROM Invoices

    A SELECT statement that computes the age of an invoice

      SELECT InvoiceDate,
         GETDATE() AS 'Today''s Date', 
         DATEDIFF(day, InvoiceDate, GETDATE()) AS Age
     
    FROM Invoices

    Description

    • An expression can include any of the functions that are supported by SQL Server. A function performs an operation and returns a value.
    • A function consists of the function name, followed by a set of parentheses that contains any parameters, or arguments, required by the function. If a function requires two or more arguments, you separate them with commas.
    • For more information on using functions, see chapter 8.

       

    More MS SQL Server Articles
    More By Murach Publishing


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





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